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 |
Fortran77
Starting Member
4 Posts |
Posted - 2012-03-21 : 09:04:17
|
Hi,I have a table which looks likes this when you use a select Statement:1 2 3 4CCAS ASPT2 200 2011/01CCAS ASPT2 200 2011/02CCAS ASPT2 100 2011/03CCAS CODI 10 2011/01CCAS CODI 10 2011/03DEL DIAI2 25 2011/01DEL DIAI2 10 2011/02DEL DIAI2 35 2011/03DEL DIAI3 20 2011/01DEL DIAI3 35 2011/02I would like to be able to manipulate the table(using T-SQL) so that the final outut looks likes this: 2011/01 2011/02 2011/031 2 3 3 3CCAS ASPT2 200 200 100CCAS CODI 10 0 10DEL DIAI2 25 10 35DEL DIAI3 20 35 0That is to tranpose some columns to rows with the corresponding values in each row.That is to have the YYYY/MM as column headdings with the correspong values from the first three rows written as one row.I hope this is clear!Any idea how to do this?Best regards,Fortran77 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-21 : 10:26:19
|
not fully clearcan you explain how you got the row 1 2 3 3 3?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Fortran77
Starting Member
4 Posts |
Posted - 2012-03-21 : 10:52:04
|
Hi,This should read like this;CCAS ASPT2 200 2011/01CCAS ASPT2 200 2011/02CCAS ASPT2 100 2011/03CCAS CODI 10 2011/01CCAS CODI 10 2011/03DEL DIAI2 25 2011/01DEL DIAI2 10 2011/02DEL DIAI2 35 2011/03DEL DIAI3 20 2011/01DEL DIAI3 35 2011/02will tranlate to: 2011/01 2011/02 2011/03CCAS ASPT2 200 200 100CCAS CODI 10 0 10DEL DIAI2 25 10 35DEL DIAI3 20 35 0I hope this clear now. |
|
|
Fortran77
Starting Member
4 Posts |
Posted - 2012-03-21 : 10:53:47
|
2011/01, 2011/02, 2011/03 are the column headings for 200, 200, 100 in the second list! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-21 : 12:13:10
|
use something likeSELECT [1],[2],MAX(CASE WHEN [4] = '2011/01' THEN [3] END) AS [2011/01],MAX(CASE WHEN [4] = '2011/02' THEN [3] END) AS [2011/02],MAX(CASE WHEN [4] = '2011/03' THEN [3] END) AS [2011/03]FROM tableGROUP BY [1],[2] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
Fortran77
Starting Member
4 Posts |
Posted - 2012-03-22 : 05:59:43
|
quote: Originally posted by visakh16 use something likeSELECT [1],[2],MAX(CASE WHEN [4] = '2011/01' THEN [3] END) AS [2011/01],MAX(CASE WHEN [4] = '2011/02' THEN [3] END) AS [2011/02],MAX(CASE WHEN [4] = '2011/03' THEN [3] END) AS [2011/03]FROM tableGROUP BY [1],[2] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Visakh16.... You are star! That is exactly what I wanted.Many many thanks for this useful hint and the SELECT staement.Deeply appreciated.Fortran77 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-22 : 16:03:52
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|