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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with query for colums into rows

Author  Topic 

pushp82
Yak Posting Veteran

83 Posts

Posted - 2013-04-03 : 12:27:22
Hi,

I have a table imported from excel(5 var fix and 5 val fix) like:
Var1 val1 var2 val2 var3 val3 var4 val4 var5 val5
Color Red Size 4 Height 2inch NULL NULL NULL NULL
Color Red Size 5 Height 2inch NULL NULL Length 1 cm
Color Red Size 4 Height 3inch NULL NULL NULL
Color Black Size 6 Height 1inch NULL NULL NULL NULL

I need the output as
Var --- Val
Color --- Red,Black
Size --- 4,5,6
Height 1inch,2inch,3inch
Length 1 cm

Var or val can be either NULL or white space. I cannot use a function.
Please help

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-03 : 15:02:58
See illustration below


CREATE TABLE #Tab
(Var1 varchar(20),
val1 varchar(20),
var2 varchar(20),
val2 varchar(20),
var3 varchar(20),
val3 varchar(20),
var4 varchar(20),
val4 varchar(20),
var5 varchar(20),
val5 varchar(20))
INSERT INTO #Tab(Var1,val1,var2,val2,var3,val3,var4,val4,var5,val5)
VALUES ('Color','Red','Size',4,'Height','2inch',NULL,NULL,NULL,NULL),
('Color','Red','Size',5,'Height','2inch',NULL,NULL,'Length','1 cm'),
('Color','Red','Size',4,'Height','3inch',NULL,NULL,NULL,''),
('Color','Black','Size',6,'Height','1inch',NULL,NULL,NULL,NULL)


SELECT DISTINCT Cat,[Var] INTo #Var
FROm (SELECT Var1,var2,var3,var4,var5 FROM #Tab)t
UNPIVOT (Cat FOR [Var] IN (Var1,var2,var3,var4,var5))u



SELECT DISTINCT Cat,Val INTO #Val
FROm (SELECT val1,val2,val3,val4,val5 FROM #Tab)t
UNPIVOT (Cat FOR Val IN (val1,val2,val3,val4,val5))u


SELECT vr.Cat AS Var,
STUFF((SELECT ',' + vl.Cat FROM #Val vl WHERE REPLACE(vr.Var,'var','') = REPLACE(Val,'Val','')
FOR XML PATH('')),1,1,'') AS Val
FROM #Var vr
ORDER BY Var

DROP TABLE #Var
DROP TABLE #val
DROP TABLE #tab

output
-----------------------------
Var Val
-----------------------------
Color Black,Red
Height 1inch,2inch,3inch
Length ,1 cm
Size 4,5,6



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

pushp82
Yak Posting Veteran

83 Posts

Posted - 2013-04-04 : 00:09:01
wow...wow...wow thanks visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-04 : 01:03:47
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

electra109
Starting Member

4 Posts

Posted - 2013-09-18 : 09:35:29
Wondered if you could help with simular issue?
I have a table with two Columns but need to output as two rows
The table shows how many users per application.
I want to show the result in a webpage

Apps1 67
Apps2 23
Apps3 66

Result in Rows
Apps1 Apps2 Apps3
67 23 66
Go to Top of Page

sigmas
Posting Yak Master

172 Posts

Posted - 2013-09-18 : 09:38:23
search for cross tabbing or pivoting
Go to Top of Page
   

- Advertisement -