Author |
Topic |
nbalraj
Starting Member
21 Posts |
Posted - 2014-01-22 : 11:34:34
|
Hi,Can you please help with the below scenario:opp table (primary table)oppid opp title1 test opp2 test opp23 test opp3bunit table (one to many table)oppid bunitid1 11 21 42 52 13 33 4dunit table (one to many table)oppid dunitid1 11 22 13 33 1lookup tablecodeid classification description1 bunit GED2 bunit GLC3 bunit CCC4 bunit DDD5 bunit SSS1 dunit iii2 dunit kkk3 dunit nnnFinal output, we needoppid title bunit dunit1 test opp GED,GLC,DDD iii,kkk2 test opp2 SSS,GED iii,nnn3 test opp3 CCC,DDD nnn,iiiThanks for any help and directions. |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-01-22 : 14:57:01
|
It took me a while to figure out the ER, but I think this query works:with opp(oppid, opp_title) as ( select * from (values (1, 'test opp'), (2, 'test opp2'), (3, 'test opp3') ) opp(oppid, opp_title) ), bunit (oppid, bunitid) as ( select * from (values (1, 1), (1, 2), (1, 4), (2, 5), (2, 1), (3, 3), (3, 4) ) bunit (oppid, bunitid) ), dunit(oppid, dunitid) as ( select * from (values (1, 1), (1, 2), (2, 1), (3, 3), (3, 1) ) dunit(oppid, dunitid) ), lookup_table(codeid, class, descr) as ( select * from (values (1, 'bunit', 'GED'), (2, 'bunit', 'GLC'), (3, 'bunit', 'CCC'), (4, 'bunit', 'DDD'), (5, 'bunit', 'SSS'), (1, 'dunit', 'iii'), (2, 'dunit', 'kkk'), (3, 'dunit', 'nnn') ) lookup_table(codeid, class, descr) ), codes (oppid, descr) as ( select b.oppid oppid, lt.descr from bunit b join lookup_table lt on b.bunitid = lt.codeid and lt.class = 'bunit' union select d.oppid, lt.descr from dunit d join lookup_table lt on d.dunitid = lt.codeid and lt.class = 'dunit' ) select opp.oppid, opp.opp_title, left(codes.descr, LEN(codes.descr) -1) as 'bunit dunit'from oppcross apply ( select codes.descr + ',' from codes where codes.oppid = opp.oppid for XML path('') ) codes(descr) I get these results:oppid opp_title bunit dunit----------- --------- -----------1 test opp DDD,GED,GLC,iii,kkk2 test opp2 GED,iii,SSS3 test opp3 CCC,DDD,iii,nnn |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-01-23 : 07:22:49
|
;with cte (oppid,bunitid,description)as(select b.oppid,b.bunitid,l.description from @bunit as binner join @lookup as lon b.bunitid = l.codeidwhere l.classification = 'bunit'),cte1(oppid,dunitid,description)as(select d.oppid,d.dunitid,l.description from @dunit as dinner join @lookup as lon d.dunitid = l.codeidwhere l.classification = 'dunit')select distinct op.opp , op.oppid , op.title , left(cte.description, LEN(cte.description) -0) as 'bunit' , left(cte1.description, LEN(cte1.description) -0) as 'dunit' from @opp as op cross apply (select ','+ c.description from cte as c where c.oppid = op.oppid for XML path('') )cte(description) cross apply (select ','+c.description from cte1 as c where c.oppid = op.oppid for XML path('') )cte1(description)Veera |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-23 : 07:23:26
|
just use a single cte instead of nested ones;With CTEAS(SELECT o.oppid, o.opptitle,b.bunitid,lb.description as [bunit],d.dunitid,ld.description as [dunit]FROM opp oLEFT JOIN bunit bON b.oppid = o.oppid LEFT JOIN lookup lbON lb.codeid = b.bunitidAND lb.classification = 'bunit'LEFT JOIN dunit dON d.oppid = o.oppid LEFT JOIN lookup ldON ld.codeid = d.dunitidAND ld.classification = 'dunit')SELECT oppid,opptitle,STUFF((SELECT ',' + bunit FROM CTE WHERE oppid = c.oppid AND bunit IS NOT NULL ORDER BY bunitid FOR XML PATH('')),1,1,'') AS bunit, STUFF((SELECT ',' + dunit FROM CTE WHERE oppid = c.oppid AND dunit IS NOT NULL ORDER BY dunitid FOR XML PATH('')),1,1,'') AS dunitFROM (SELECT DISTINCT oppid,opptitle FROM CTE) c ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-01-23 : 08:52:57
|
quote: Originally posted by visakh16 just use a single cte instead of nested ones...snip
that doesn't work correctly, I think. When I run your version I get:oppid opp_title bunit dunit----------- --------- ---------------------- ------------------------1 test opp GED,GED,GLC,GLC,DDD,DDD iii,iii,iii,kkk,kkk,kkk2 test opp2 GED,SSS iii,iii3 test opp3 CCC,CCC,DDD,DDD |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-23 : 13:42:09
|
that can be dealt with by a simple tweaksee belowdeclare @opp table (oppid int,opp varchar(30),title varchar(10))insert @oppvalues(1,'test','opp'),(2,'test','opp2'),(3,'test','opp3')declare @bunit table (oppid int,bunitid int)insert @bunitvalues(1, 1),(1, 2),(1, 4),(2, 5),(2, 1),(3, 3),(3, 4)declare @dunit table (oppid int, dunitid int)insert @dunitvalues(1, 1),(1, 2),(2, 1),(3, 3),(3, 1)declare @lookup table(codeid int,classification varchar(50),[description] varchar(50))insert @lookupvalues(1,'bunit','GED'),(2,'bunit','GLC'),(3,'bunit','CCC'),(4,'bunit','DDD'),(5,'bunit','SSS'),(1,'dunit','iii'),(2,'dunit','kkk'),(3,'dunit','nnn');With CTEAS(SELECT o.oppid, o.opp,title,b.bunitid,lb.description as [bunit],d.dunitid,ld.description as [dunit]FROM @opp oLEFT JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY oppid ORDER BY bunitid) AS Rn,* FROM @bunit) bON b.oppid = o.oppid LEFT JOIN @lookup lbON lb.codeid = b.bunitidAND lb.classification = 'bunit'LEFT JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY oppid ORDER BY dunitid) AS Rn,* FROM @dunit) dON d.oppid = o.oppid AND d.Rn = b.RnLEFT JOIN @lookup ldON ld.codeid = d.dunitidAND ld.classification = 'dunit')SELECT oppid,opp,title,STUFF((SELECT ',' + bunit FROM CTE WHERE oppid = c.oppid AND bunit IS NOT NULL ORDER BY bunitid FOR XML PATH('')),1,1,'') AS bunit, STUFF((SELECT ',' + dunit FROM CTE WHERE oppid = c.oppid AND dunit IS NOT NULL ORDER BY dunitid FOR XML PATH('')),1,1,'') AS dunitFROM (SELECT DISTINCT oppid,opp,title FROM CTE) coutput-------------------------------------------------------oppid opp title bunit dunit-------------------------------------------------------1 test opp GED,GLC,DDD iii,kkk2 test opp2 GED,SSS iii3 test opp3 CCC,DDD iii,nnn ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|