A.Goutam
A.Goutam

Reputation: 3494

How to sum the value for each

I have table following :

ClientNUM    PIECES   DID
NEWAGENC     10       5
NEWAGENC     25       5
WAY          30       4
CHCAH        20       2
AVC          21       2

i want the Result that sum the value for each client as below

 CleintNUM     Pieces    DID
  NEWAGENC      35        5
  WAY           30        4
  CHCAH         20        2
  AVC          21         2

My query

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

So how can i do the sum for each CLIENTNUM in my query Means DISTINCT For each client Pieces like NEWAGENC has value 10 and in second row 25 so the pieces will be 10+ 25 = 35

Upvotes: 1

Views: 34

Answers (1)

juergen d
juergen d

Reputation: 204766

Don't group by PIECES if you want to aggregate it

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

Upvotes: 1

Related Questions