x
login about faq Site discussion (meta-askssc)

SQL Query Help needed for PIVOTing a table

Hello All I have an intermediate table result which looks like this

ID                  Header                     CONTENT                    
------------------------------------------------------------                    
GUID 1              Header1                    123456                     
GUID 1              Header2                    Phone                    
GUID 1              Header3                    Main Street                    
GUID 2              Header1                    654321                     
GUID 2              Header2                    Phone                    
GUID 3              Header3                    Some Street                    

I need to transform this result set into something like this

ID        Header1    Header2     Header3                                        
----------------------------------------                    
GUID 1    123456     Phone       Main Street                     
GUID 2    654321     Phone       Some Street                    

I guess i have to use PIVOTing for the same. But im stuck in the point where i cannot use the COUNT/SUM operator inorder to get the CONTENT column.Can anyone help me with the query for achieving the result ???

Thanks in advance

more ▼

asked Oct 27 '09 at 11:02 AM in Default

rainmaker gravatar image

rainmaker
12 1 2 2

can you post your sample data in the form of
declare @test table ( . . . )
insert into @test ...
insert into @test ...

i can't tell which data belongs to which column from what you have posted

Oct 27 '09 at 11:03 PM Squirrel 1

rainmaker, any feedback on the code I posted?

Oct 31 '09 at 04:07 AM Madhivanan
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
declare @t table(ID varchar(100),Header varchar(100),CONTENT varchar(100))            
insert into @t            
select 'GUID 1', 'Header1','123456' union all            
select 'GUID 1', 'Header2','Phone' union all            
select 'GUID 1', 'Header3','Main Street' union all            
select 'GUID 2', 'Header1','654321' union all            
select 'GUID 2', 'Header2','Phone' union all            
select 'GUID 3', 'Header3','Some Street'            
            
select * from            
(            
    select id,header,content from @t            
) as t            
pivot            
(            
    max(Content) for Header in ([Header1],[Header2],[Header3])            
) as p            
more ▼

answered Oct 28 '09 at 03:53 AM

Madhivanan gravatar image

Madhivanan
1.1k 1 2 6

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1834
x65

asked: Oct 27 '09 at 11:02 AM

Seen: 468 times

Last Updated: Oct 28 '09 at 09:13 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.