1

请参见下面的示例。如果 id, A, B, D 与其他记录相同,则 union 和 sum 列 "C" 。

my data:
id A  B  C  D
---------------------------
1  1  2  3  4
2  aa bb 10 dd
3  1  2  3  4
4  1  2  3  4
2  aa bb 30 dd

how to do after like below result:
id A  B  C  D
---------------------------
1  1  2  3  4
2  aa bb 40 dd    <----if id,A,B,D are same with other records,then union and sum column "C"
3  1  2  3  4
4  1  2  3  4

I tried to union all records,but fail and show"
id A  B  C  D
---------------------------
2  aa bb 49 dd 
4

1 回答 1

0

只需像这样GROUP BY ID, A, B, D使用:SUM(C)

SELECT id, A,  B,  SUM(C) AS C,  D
FROM Tablename
GROUP BY ID, A, B, D;

SQL 小提琴演示

于 2013-03-04T09:50:58.480 回答