Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


pivot? can't make it work.


pivot? can't make it work.

Author
Message
snomadj
snomadj
SSC-Enthusiastic
SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)

Group: General Forum Members
Points: 158 Visits: 616
Hi there,

I'm hoping for a pointer in the right direction as I can't suss this out & feel that it shouldn't be that complicated.

I have a table

id characteristic value(nvarhcar)
1 color blue
1 age 5
1 gender female
2 color green
2 cost 5
3 desc blahblah
3 lastcheck 27/jan/09
3 ...
3 ...

Client wants output as follows:

id characteristic1 value1 characteristic2 value2 ...
1 color blue age
2 color green desc blah

There is no aggregate.

Ideally query would by dynamic as new characteristics values may be added. not all id's have same number of characteristic values.

does this make sense?

any ideas / pointers gratefully accepted?

thanks.
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3049 Visits: 3633
PIVOT will give you the results for each ID in one single row:

if OBJECT_ID('tempdb..#temp') is not null
drop table #temp
create table #temp (id int, characteristic varchar(15), value nvarchar(15))
insert into #temp
select 1, 'color', 'blue'
union all
select 1, 'age', '5'
union all
select 1, 'gender', 'male'
union all
select 2, 'color', 'green'
union all
select 2, 'cost', '10.0'
union all
select 2, 'description', 'testing'
union all
select 3, 'age', '7'
union all
select 3, 'gender', 'female'
union all
select 3, 'description', 'something'

select *
from #temp
pivot
(max(value)
for characteristic in ([color],[age],[gender],[cost],[description])
) as pvtTable

if OBJECT_ID('tempdb..#temp') is not null
drop table #temp



Expand the list in "for characteristic in ([color],[age],[gender],[cost],[description])" with all possible options. In your sample data it is not completly clear what all values are, so that's up to you. When there is no row present for a specific characteristic a NULL value will be shown in the resultset.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
snomadj
snomadj
SSC-Enthusiastic
SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)

Group: General Forum Members
Points: 158 Visits: 616
Thanks, it seems like a complicated solution to what I thought would be simpler.

I'll have to make it dynamic because the 'characteristic' values may change, but, presuming there's no easier way, thanks for the taking the time to reply.
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3049 Visits: 3633
Take a look at the blog from Aaron Bertrand where he builds a dynamic PIVOT solution:
http://www.mssqltips.com/sqlservertip/2783/script-to-create-dynamic-pivot-queries-in-sql-server/

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3001 Visits: 4995
Do you find this simpler?
SELECT   id,
MAX( CASE WHEN characteristic = 'color' THEN value ELSE NULL END ) AS color,
MAX( CASE WHEN characteristic = 'age' THEN value ELSE NULL END ) AS age,
MAX( CASE WHEN characteristic = 'gender' THEN value ELSE NULL END ) AS gender,
MAX( CASE WHEN characteristic = 'cost' THEN value ELSE NULL END ) AS cost,
MAX( CASE WHEN characteristic = 'description' THEN value ELSE NULL END ) AS description
FROM #temp AS t
GROUP BY id



If Yes, these are called CROSS TABS and you can find more information on this technique below
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search