如何总结每个的价值

时间:2016-05-13 10:11:35

标签: sql-server-2008 stored-procedures

我有以下表格:

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

1 个答案:

答案 0 :(得分:1)

如果要汇总,请不要按PIECES进行分组

SELECT CLIENTNUM,
       DID, 
       PIECES,   
       GETDATE() AS CURRENTDATE, 
       SUM(PIECES)
FROM  Mytable
GROUP BY CLIENTNUM, DISPID