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 2005 Forums
 Transact-SQL (2005)
 need help in displaying row results as columns???

Author  Topic 

raritan
Starting Member

39 Posts

Posted - 2009-10-16 : 17:36:14
I have a table with the following columns:

AccountID | Year | Month | Balance

Let's say that the table contains the following data:

AccountID | Year | Month | Balance
1130.0000 | 2009 | 03 | 200.00
1130.0000 | 2009 | 06 | 200.00
1130.0000 | 2009 | 09 | 200.00
2230.0000 | 2009 | 03 | 200.00
2230.0000 | 2009 | 06 | 200.00
2230.0000 | 2009 | 09 | 200.00


Is there a way I can get the results to display like this:

AccountID | Year | 03Balance | 06Balance | 09Balance
1130.0000 | 2009 | 100.00 | 200.00 | 300.00
2230.0000 | 2009 | 400.00 | 500.00 | 600.00


Thank you for your help,
Kevin

singularity
Posting Yak Master

153 Posts

Posted - 2009-10-16 : 20:21:40
[code]
select AccountID, Year,
sum(case when [Month] = '03' then Balance else 0 end) as Balance03,
sum(case when [Month] = '06' then Balance else 0 end) as Balance06,
sum(case when [Month] = '09' then Balance else 0 end) as Balance09
from yourtable
group by AccountID, Year
[/code]
Go to Top of Page

raritan
Starting Member

39 Posts

Posted - 2009-10-17 : 10:56:46
Thanks for the help - I will run this Monday and post back
Go to Top of Page

raritan
Starting Member

39 Posts

Posted - 2009-10-19 : 10:32:03
This worked well, thank you very much for your help.
Go to Top of Page
   

- Advertisement -