|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-21 : 09:40:19
|
| [code]-- Prepare sample dataDECLARE @Lookup TABLE (Sector VARCHAR(5), Type VARCHAR(2), Ident VARCHAR(4), Dc VARCHAR(3))INSERT @LookupSELECT 'EA11A', 'AA', 'XYZ', 'WW1' UNION ALLSELECT 'EA11A', 'AA', 'ABC', 'WW1' UNION ALLSELECT 'CE12B', 'AA', 'CBAA', 'WW2' UNION ALLSELECT 'CE12B', 'BB', 'DEF', 'WW3'DECLARE @Existing TABLE (Sector VARCHAR(5), Type VARCHAR(2), Ident VARCHAR(4))INSERT @ExistingSELECT 'EA11A', 'AA', 'XYZ' UNION ALLSELECT 'EA11A', 'AA', 'XYZ' UNION ALLSELECT 'EA11A', 'AB', 'XYZ' UNION ALLSELECT 'EA11B', 'AA', 'ABC' UNION ALLSELECT 'CE12B', 'AA', 'CBAA' UNION ALLSELECT 'CE12B', 'BB', 'DEF' UNION ALLSELECT 'CE12B', 'BB', 'DEF'-- Show the expected output, take 1SELECT DISTINCT e.Sector, e.Type, e.IdentFROM @Existing AS eINNER JOIN @Lookup AS l ON l.Sector = e.Sector AND l.DC = 'WW1'UNION ALLSELECT DISTINCT e.Sector, e.Type, e.IdentFROM @Existing AS eINNER JOIN @Lookup AS l ON l.Type = e.Type AND l.Ident = e.Ident AND l.DC = 'WW1'ORDER BY Sector, Type, Ident-- Show the expected output, take 2SELECT Sector, Type, IdentFROM ( SELECT 0 AS Diff, e.Sector, e.Type, e.Ident FROM @Existing AS e INNER JOIN @Lookup AS l ON l.Sector = e.Sector AND l.DC = 'WW1' UNION SELECT 1, e.Sector, e.Type, e.Ident FROM @Existing AS e INNER JOIN @Lookup AS l ON l.Type = e.Type AND l.Ident = e.Ident AND l.DC = 'WW1' ) AS xORDER BY Sector, Type, Ident[/code]Peter LarssonHelsingborg, Sweden |
 |
|