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 |
|
pmotewar
Yak Posting Veteran
62 Posts |
Posted - 2009-05-14 : 04:52:34
|
Hi all, my table having the date in following wayR_ID R_Date R_Value1 10-Mar-07 101 11-Mar-07 11 12-Mar-07 111 14-Mar-07 231 17-Mar-07 441 18-Mar-07 231 19-Mar-07 541 20-Mar-07 241 21-Mar-07 661 22-Mar-07 341 23-Mar-07 251 24-Mar-07 331 25-Mar-07 671 26-Mar-07 352 10-Mar-07 442 11-Mar-07 5552 12-Mar-07 642 14-Mar-07 352 17-Mar-07 77652 18-Mar-07 3542 19-Mar-07 352 20-Mar-07 662 21-Mar-07 532 22-Mar-07 5672 23-Mar-07 4332 24-Mar-07 5672 25-Mar-07 3562 26-Mar-07 356and i want the out put following in mannerR_Date 1 210-Mar-07 10 4411-Mar-07 1 55512-Mar-07 11 6414-Mar-07 23 3517-Mar-07 44 776518-Mar-07 23 35419-Mar-07 54 3520-Mar-07 24 6621-Mar-07 66 5322-Mar-07 34 56723-Mar-07 25 43324-Mar-07 33 56725-Mar-07 67 35626-Mar-07 35 356how can i write the select query to get desired output. Pankaj |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-14 : 04:55:28
|
SELECT R_DATE, SUM(CASE WHEN R_ID = 1 THEN R_Value ELSE 0 END) AS [1],SUM(CASE WHEN R_ID = 2 THEN R_Value ELSE 0 END) AS [2]FROM Table1GROUP BY R_DATEORDER BY R_DATE E 12°55'05.63"N 56°04'39.26" |
 |
|
|
pmotewar
Yak Posting Veteran
62 Posts |
Posted - 2009-05-14 : 05:31:46
|
quote: Originally posted by Peso SELECT R_DATE, SUM(CASE WHEN R_ID = 1 THEN R_Value ELSE 0 END) AS [1],SUM(CASE WHEN R_ID = 2 THEN R_Value ELSE 0 END) AS [2]FROM Table1GROUP BY R_DATEORDER BY R_DATE E 12°55'05.63"N 56°04'39.26"
Hi peso, above query gives correct output but my table having lots of R_id's so, is there any need to attach the statement for each ID ? also each R_id have the name and i want to show this R_Name instead of R_ID.Pankaj |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-14 : 05:38:47
|
Yes.SQL Server does not support dynamic pivoting.You'll have to write or re-use an existing solution for dynamic pivoting. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-14 : 07:05:31
|
Hurrah!The Madhivanan bot is up again ;-) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|