Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Pivot help

Author  Topic 

w1102157
Yak Posting Veteran

80 Posts

Posted - 2012-07-15 : 17:31:59
Hi Guys

i am doing a task at work where i need to present the data in a pivot for example

currently the data is like the blow

type Name count
open TT 567
open AA 56
closed TT 34
closed AA 100

i need to present the above data like

TT TT AA AA
open closed Open Closed
567 34 56 100

Is there any way i can do this?

thank you

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-15 : 22:01:16
[code]
SELECT SUM(CASE WHEN type = 'open' AND Name ='TT' THEN [count] ELSE 0 END) AS TTOpen,
SUM(CASE WHEN type = 'closed' AND Name ='TT' THEN [count] ELSE 0 END) AS TTClosed,
SUM(CASE WHEN type = 'open' AND Name ='AA' THEN [count] ELSE 0 END) AS AAOpen,
SUM(CASE WHEN type = 'closed' AND Name ='AA' THEN [count] ELSE 0 END) AS AAClosed
FROM Table
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -