Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2015-02-18 : 08:32:16
|
Hi, I have the below scenariocreate table #comp(compid int,compname1 varchar(100),compname2 varchar(100))insert into #comp values(001,'abc','abc')insert into #comp values(002,'xyz','efg')insert into #comp values(003,'lmn',null)insert into #comp values(004,null,'pqr')insert into #comp values(005,null,null)create table #installs(compid int,product varchar(100))insert into #installs values(001,'Adobe')insert into #installs values(002,'Excel')insert into #installs values(003,'Word')insert into #installs values(004,'Powerpoint')insert into #installs values(005,'SQLSERVER')-- USED QUERY - NOT GIVING SATISFACTORY RESULTselect distinct c.compid,c.compname1,i.productfrom #comp cjoin #installs i on c.compid = i.compidunionselect distinct c.compid,c.compname2,i.productfrom #comp cjoin #installs i on c.compid = i.compid-- ABOVE QUERY RESULT1 abc Adobe2 efg Excel2 xyz Excel3 NULL Word3 lmn Word4 NULL Powerpoint4 pqr Powerpoint5 NULL SQLSERVER-- DESIRED RESULT SHOULD BE1 abc Adobe2 efg Excel2 xyz Excel3 lmn Word4 pqr Powerpoint5 NULL SQLSERVER |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-02-18 : 09:57:11
|
Why do you want to see NULL for SQLSERVER but not for Word or Powerpoint? |
|
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2015-03-04 : 10:10:38
|
thats because word and powerpoint have a valid comp name....since sql server is not having any comp name we still need to keep that. |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2015-03-04 : 11:28:12
|
[code]WITH NormalizedAS( SELECT DISTINCT compid, compname FROM ( SELECT compid, compname1, compname2 FROM #comp ) P UNPIVOT ( compname FOR Comp IN (compname1, compname2) ) U)SELECT I.compid, N.compname, I.productFROM #installs I LEFT JOIN Normalized N ON I.compid = N.compid;[/code] |
|
|
|
|
|
|
|