x

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, 2009 at 11:02 AM in Default

avatar image

rainmaker
12 2 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, 2009 at 11:03 PM Squirrel 1

rainmaker, any feedback on the code I posted?

Oct 31, 2009 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, 2009 at 03:53 AM

avatar image

Madhivanan
1.1k 2 5 9

(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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x2017
x104

asked: Oct 27, 2009 at 11:02 AM

Seen: 952 times

Last Updated: Oct 28, 2009 at 09:13 AM

Copyright 2016 Redgate Software. Privacy Policy