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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Pivoting data

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-09-13 : 11:00:51
[code]

Hi All,

I have following table.In Which for each status there is daily count
ctr is to determine first day ,second day and so on .Currently I found only two status It might be more like failed to mapped,Reissued. But right now I am not bother about them.

ctr Status StatusID dailyCount
1 Item ready 10 85
1 Cancelled 550 12
2 Item ready 10 83
2 Cancelled 550 8
3 Item ready 10 79
3 Cancelled 550 6
4 Item ready 10 76
4 Cancelled 550 4


I am expecting result in below format I tried to implement using dynamic Pivot but result was not expected .Could someone guild me on this ..
Ctr Item ready Cancelled
1 85 12
2 83 8
3 79 6
4 76 4

[/code]



Vijay is here to learn something from you guys.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-13 : 11:14:07
[code]
SELECT *
FROM (SELECT ctr,Status,dailyCount FROM Table) t
PIVOT (SUM(dailyCount) FOR Status IN ([Item ready],[Cancelled]))p
[/code]

to make it dynamic use

http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

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

Go to Top of Page
   

- Advertisement -