locked
SQL SUM A COLUMN RRS feed

  • Question

  • I need to sum the last column for each distinct name column

      

            
        DECLARE @t TABLE(NAME NVARCHAR(MAX),date date,val money)
       
        insert INTO @t SELECT 'b','2012-01-05',100
        insert INTO @t SELECT 'b','2012-01-06',200
        insert INTO @t SELECT 'b','2012-01-07',200
        insert INTO @t SELECT 'd','2012-01-07',400
        insert INTO @t SELECT 'd','2012-01-09',500
        insert INTO @t SELECT 'd','2012-01-12',600
       
    SELECT  Name,datee,SUM (val) total
      from @t
      GROUP BY NAME, datee
           
    Here is the desired outcome:

        Name money
        b    500
        d    1500

    This returns

    • Edited by Kidus05 Tuesday, May 28, 2013 11:05 PM
    Tuesday, May 28, 2013 10:37 PM

Answers

  • Select Name, Sum(Val) From @t Group By name 
    
    --output
    Name	(No column name)
    b	500.00
    d	1500.00


    Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered.

    • Proposed as answer by Naomi N Tuesday, May 28, 2013 11:00 PM
    • Marked as answer by Kidus05 Tuesday, May 28, 2013 11:03 PM
    Tuesday, May 28, 2013 10:41 PM

All replies

  • Select Name, Sum(Val) From @t Group By name 
    
    --output
    Name	(No column name)
    b	500.00
    d	1500.00


    Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered.

    • Proposed as answer by Naomi N Tuesday, May 28, 2013 11:00 PM
    • Marked as answer by Kidus05 Tuesday, May 28, 2013 11:03 PM
    Tuesday, May 28, 2013 10:41 PM
  • thanks Arbi!
    • Edited by Kidus05 Tuesday, May 28, 2013 11:04 PM
    Tuesday, May 28, 2013 11:04 PM