Please try this:
CREATE TABLE #Final ( RwNum INT PRIMARY KEY, JobSource NVARCHAR(100) , RuleName NVARCHAR(100) ,PackageType NVARCHAR(100) )
INSERT #Final
( RwNum ,
JobSource ,
RuleName ,
PackageType
)
VALUES ( 1 , N'Presubmission' , N'GameRating' , N'Xap' ), ( 2 , N'PostSubmission' , N'GameRating' , N'Xap' ), ( 3 , N'Presubmission' , N'GameRating' , NULL ),
( 4 , N'Presubmission' , N'TCRRule' , N'Xap' ), ( 5 , N'PostSubmission' , NULL , N'Xap' ), ( 6 , N'Submission' , NULL , N'Xap' ) ;
SELECT *
FROM #Final a
SELECT a.RwNum AS aId, b.RwNum AS bId
FROM #Final a
JOIN #Final b
ON ( a.JobSource = b.JobSource AND a.RuleName = b.RuleName )
OR ( a.JobSource = b.JobSource AND a.PackageType = b.PackageType AND a.RuleName IS NULL )
WHERE a.RwNum <> b.RwNum
SELECT *
FROM #Final AS f
WHERE NOT EXISTS
( SELECT *
FROM #Final AS a
JOIN #Final AS b
ON ( a.JobSource = b.JobSource AND a.RuleName = b.RuleName )
OR ( a.JobSource = b.JobSource AND a.PackageType = b.PackageType AND a.RuleName IS NULL )
WHERE a.RwNum <> b.RwNum
AND f.RwNum = IIF(a.RwNum > b.RwNum, a.RwNum , b.RwNum) )
Saeid Hasani [sqldevelop]
No comments:
Post a Comment