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.
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 Amount1 01/04/09 Strip £10.20 1 01/04/09 Cons £97.401 06/05/09 Cons £45.602 09/05/09 Exp £69.002 21//05/09 Strip £15.50I 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.ThanksPaul |
|
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 PaymentsGROUP BY ProjID, DateORDER BY ProjID, Date E 12°55'05.63"N 56°04'39.26" |
|
|
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.RegardsPaul |
|
|
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 |
|
|
|
|
|