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 |
|
imughal
Posting Yak Master
192 Posts |
Posted - 2009-10-10 : 00:23:10
|
| hi,i want to show my row data in columns. my table structure is as follows:Name Field1 Fieldname Date Timeabc 220 P1 7/09/09 9:00abc 230 P2 7/09/09 9:00abc 250 P3 7/09/09 9:00abc 210 P1 7/09/09 9:20abc 205 P2 7/09/09 9:20abc 200 P3 7/09/09 9:20abc 210 P1 7/09/09 9:45abc 205 P2 7/09/09 9:45abc 200 P3 7/09/09 9:45I want to my result as followName P1 P2 p3 Date Time SUMabc 220 230 250 7/09/09 9:00 700abc 210 205 200 7/09/09 9:20 615abc 210 205 200 7/09/09 9:45 615kindly help me out to write query to produce desire result.thx |
|
|
Kabila
Starting Member
33 Posts |
Posted - 2009-10-10 : 01:15:54
|
| SELECT Name, [P1] AS P1, [P2] AS P2,[P3] AS P3,Date,[P1]+[P2]+[P3] as Sum FROM (SELECT * from tablename ) psPIVOT(sum(Field1)FOR Fieldname IN( [P1], [P2],[P3]) )AS A |
 |
|
|
imughal
Posting Yak Master
192 Posts |
Posted - 2009-10-10 : 03:28:02
|
| thx, jut little problem my "Field1" data type is string when i converting it to int getting error, pls help me outSELECT Name,[P1] AS P1, [P2] AS P2,[P3] AS P3,Date,[P1]+[P2]+[P3] as SumFROM(SELECT * from tablename ) psPIVOT(sum((CONVERT(int,Field1))FOR Fieldname IN( [P1], [P2],[P3]) )AS A |
 |
|
|
|
|
|
|
|