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


pivot query help


pivot query help

Author
Message
AR-777
AR-777
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 285
This is my query

select year, ID, val
from table1 t1
left join table2 t2 on t1.companyid = t2.companyid

the output of the query is :

year    ID      val

2008 7 NULL
2009 7 0.00
2010 7 0.00
2011 7 0.00
2008 8 NULL
2009 8 0.00
2010 8 0.00
2011 8 0.00


now i want to pivot this table to get the output as below

year    7      8
2008 null null
2009 0.00 0.00
2010 0.00 0.00
2011 0.00 0.00


Now there can be many numbers of companyid so the number of colums in the final pivot query will increase or decrease based on distinct Id numbers.

Can you please help me with this.

thanks
Nabha
Nabha
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 Visits: 1814
Sorry, I aint givin the complete solution. This I guess you would anyway have it. I am really interested to know how anyone will approach this other part of the problem (unknown number of pivot columns)


CREATE TABLE #T3(iYear int, ID int , Val float)

INSERT INTO #T3 VALUES (2008, 7, NULL)
INSERT INTO #T3 VALUES (2009, 7, 0.0)
INSERT INTO #T3 VALUES (2010, 7, 0.0)
INSERT INTO #T3 VALUES (2011, 7, 0.0)
INSERT INTO #T3 VALUES (2008, 8, NULL)
INSERT INTO #T3 VALUES (2009, 8, 0.0)
INSERT INTO #T3 VALUES (2010, 8, 0.0)
INSERT INTO #T3 VALUES (2011, 8, 0.0)

select
iYear, [7], [8]
from #t3 T
PIVOT (sum(Val) FOR ID IN ([7],[8])) PVT



---------------------------------------------------------------------------------
Garadin
Garadin
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1523 Visits: 4107
You basically need to use dynamic SQL to generate the columns for the pivot. Jeff wrote an article on it here:

http://www.sqlservercentral.com/articles/Crosstab/65048/

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
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