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 2000 Forums
 Transact-SQL (2000)
 Transpose Rows to Column

Author  Topic 

pe1826
Starting Member

6 Posts

Posted - 2009-06-25 : 09:02:28
Hi all,

I hope some one will be able to help.

I have a table(Payments)in SQL2000 database which contains data in the following format.

ProjID Date Category Amount
1 01/04/09 Strip £10.20
1 01/04/09 Cons £97.40
1 06/05/09 Cons £45.60
2 09/05/09 Exp £69.00
2 21//05/09 Strip £15.50

I would like if possible to transpose the Category and Amount columns to show the data in the following way.

ProjID Date Strip Cons Exp
1 01/04/09 £10.20 ____ ____
1 01/04/09 ____ £97.40 ____
1 06/05/09 ____ £45.60
2 09/05/09 ____ ____ £69.00
2 21/05/09 £15.50 ____ ____

I hope this makes sense.

I have had a number of goes in different ways of doing this having looked for solutions on the internet but cannot get it right.

Any suggestions/help would be much appreciated.

Thanks
Paul

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-25 : 09:04:47
SELECT ProjID, Date,
MAX(CASE WHEN Category = 'Strip' THEN Amount ELSE NULL END) AS [Strip],
MAX(CASE WHEN Category = 'Cons' THEN Amount ELSE NULL END) AS [Cons],
MAX(CASE WHEN Category = 'Exp' THEN Amount ELSE NULL END) AS [Exp]
FROM Payments
GROUP BY ProjID, Date
ORDER BY ProjID, Date



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

pe1826
Starting Member

6 Posts

Posted - 2009-06-25 : 10:10:00
Many thanks Peso for your quick and spot on solution, it worked a treat.

Regards
Paul
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-06-26 : 00:43:03
see this link
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx
Go to Top of Page
   

- Advertisement -