我有以下表格:
ClientNUM PIECES DID
NEWAGENC 10 5
NEWAGENC 25 5
WAY 30 4
CHCAH 20 2
AVC 21 2
我想要结果,将每个客户的价值加总,如下所示
CleintNUM Pieces DID
NEWAGENC 35 5
WAY 30 4
CHCAH 20 2
AVC 21 2
我的查询
SELECT
CLIENTNUM,
DID,
PIECES,
GETDATE() AS CURRENTDATE,
SUM(PIECES)
FROM Mytable
GROUP BY CLIENTNUM, DISPID, PIECES
那么如何才能在我的查询中为每个CLIENTNUM
做总和。手段DISTINCT对于每个客户端,像NEWAGENC这样的零件值为10,第二排25件则为10 + 25 = 35
答案 0 :(得分:1)
如果要汇总,请不要按PIECES
进行分组
SELECT CLIENTNUM,
DID,
PIECES,
GETDATE() AS CURRENTDATE,
SUM(PIECES)
FROM Mytable
GROUP BY CLIENTNUM, DISPID