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)
 Month results in different columns

Author  Topic 

keesvo
Starting Member

10 Posts

Posted - 2015-03-24 : 11:35:25
Hello,

I have a query which shows the results in the different rows. But I want the results of the month in columns instead of rows. Let make my question clear with the following example. Below my query and my results:

query:
select
klaref,
bkjref,
monthref,
case when monthref = '01' then SUM(amount) end as jan,
case when monthref = '02' then sum(amount) end as feb,
case when monthref = '03' then sum(amount) end as mrt
from invoices
group by klaref, bkjref, periref

the result is as following:

klaref bkjref monthref jan feb mrt
100001 2015 01 500
100001 2015 02 300
100001 2015 03 900

But what I want is the following result:

klaref bkjref jan feb mrt
100001 2015 500 300 900

Does someone know how I can realize this.
thank you !

MichaelJSQL
Constraint Violating Yak Guru

252 Posts

Posted - 2015-03-24 : 11:42:58
Use unpivot
https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
Go to Top of Page

keesvo
Starting Member

10 Posts

Posted - 2015-03-31 : 07:28:39
Thanx.
That did the trick.
Go to Top of Page
   

- Advertisement -