在sql总计

时间:2011-08-11 01:31:07

标签: sql-server-2005

Relation    hhc101  hhc166  hhc201  total
1           1435        18       1  1455
2            430         2       0   434
3           1121         0       0  1124
Grand total 2986        20       1

我是SQL的新手。我想要得到这张桌子。请帮我拿这张桌子。 谢谢 我的原始表是

GrpNum  MemberNum   Relation
HHC101   397            3
HHC101   663            1
HHC101   707            2
HHC101   903            3
HHC166  1206            3
hhc166  1326            2
HHC166  1800            1
HHC201  1839            3
HHC201  1951            1
HHC101  2098            1
HHC101  2146            3

1 个答案:

答案 0 :(得分:0)

我最好的猜测,如下所示。对不起,我在家用电脑上没有任何工具可以试试这个。在MSDN中查找PIVOT以获取更多信息

SELECT Relation, [HHC101] AS HHC101, [HHC166] AS HHC166, [HHC101] AS HHC101
FROM 
(SELECT Relation, GrpNum, MemberNum
FROM your_table_name_here ) p
PIVOT
(
SUM (MemberNum)
FOR Relation IN
( [HHC101], [HHC166], [HHC101] )
) AS pvt
ORDER BY pvt.Relation;