SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with query for colums into rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pushp82
Yak Posting Veteran

83 Posts

Posted - 04/03/2013 :  12:27:22  Show Profile  Reply with Quote
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

Edited by - pushp82 on 04/03/2013 12:30:29

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/03/2013 :  15:02:58  Show Profile  Reply with Quote
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 - 04/04/2013 :  00:09:01  Show Profile  Reply with Quote
wow...wow...wow thanks visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/04/2013 :  01:03:47  Show Profile  Reply with Quote
welcome

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

electra109
Starting Member

4 Posts

Posted - 09/18/2013 :  09:35:29  Show Profile  Reply with Quote
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

Belarus
172 Posts

Posted - 09/18/2013 :  09:38:23  Show Profile  Reply with Quote
search for cross tabbing or pivoting
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000