Wednesday, November 27, 2013

Left Join Issue

Try this,



declare @table1 table(Sid int, SUb char(2))
insert into @table1 values(201,'T1')
insert into @table1 values(201,'T2')
insert into @table1 values(201,'T5')
insert into @table1 values(202,'T2')
insert into @table1 values(202,'T3')
insert into @table1 values(203,'T4')
-------------------------------
declare @table2 table(Subs char(2))
insert into @table2 values('T1')
insert into @table2 values('T2')
insert into @table2 values('T3')
insert into @table2 values('T4')
insert into @table2 values('T5')
-------------------------------
;with cte_cross as (
select distinct a.Sid,b.Subs from @table1 a cross join @table2 b
),cte_validation as (
select Subs, case
when not exists(select * from @table1 a where a.Sid = c.Sid and a.Sub = c.Subs) then null
else Sid end newSid
from cte_cross c
)
select * from cte_validation





Regards, RSingh


No comments:

Post a Comment