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 |
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-17 : 06:10:37
|
hi all i have table like thisPID Project val11 A 81 A 892 C 72 A 792 A 93 B 903 A 73 C 873 A 7893 A 8974 B 74 A 794 B 94 B 905 C 75 B 875 B 7895 B 8976 C 76 C 796 C 96 A 906 C 77 A 877 B 7897 C 7 and i want the result like this (sum or count)PID A B C1 97 2 88 73 1693 90 874 79 106 5 1773 76 90 1027 87 789 7 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-17 : 06:19:04
|
Are you using SQL 2000 or 2005/2008 ?For 2005/2008 use the PIVOT operator KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-07-17 : 06:19:28
|
| Use the PIVOT command.http://msdn.microsoft.com/en-us/library/ms177410.aspx |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-17 : 06:19:41
|
| SQL 2000 |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-17 : 06:20:45
|
| SQL 2000 (prod) 2005 (test )i need for both esp. for 2000and also can i create a view with group by fucntion |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-17 : 06:34:35
|
| in 2000 u can use dynamic cross taband 2005 and above use pivotsee this linkhttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-17 : 06:35:13
|
[code]SELECT PID, [A], [B], [C]FROM( SELECT PID, Project, val1 FROM table)aPIVOT( SUM(val1) FOR Project IN ([A], [B], [C])) as pvtSELECT PID, SUM(CASE WHEN Project = 'A' then val1 else 0 END)[A], SUM(CASE WHEN Project = 'B' then val1 else 0 END)[B], SUM(CASE WHEN Project = 'C' then val1 else 0 END)[C]FROM table[/code] Hope can help...but advise to wait pros with confirmation... |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-17 : 06:41:21
|
| [code]first one will work in 2005 and abovesecond one will work in 2000 ,2005,2008select pid,[a],[b],[c]from @tab spivot (sum(val1) for project in ([a],[b],[c]))pselect pid,sum(case when project ='a' then val1 end ) as 'a',sum(case when project ='b' then val1 end ) as 'b',sum(case when project ='c' then val1 end ) as 'c'from @tab group by pidsorry toooooooo late [/code] |
 |
|
|
|
|
|
|
|