Sum of Column When Values Different

  • Dear Forum:

    I have a Column1 that has values from 1 - 9.  I need to Sum the values of this column for each value (1 through 9) when Column2 = '1'

    So I the results would be:

    1 = 23

    2 = 43

    3 = 96

    .....

    Would appreciate some help on how to accomplish this.

  • SELECT 

     SUM(CASE WHEN COLUUMN1 = 1 THE COLUMN1 ELSE 0 END),

     SUM(CASE WHEN COLUUMN1 = 2 THE COLUMN1 ELSE 0 END),

    And so forth

    FROM YourTable

    WHERE Column2 = 1

     

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  •  

    SELECT Column1, COUNT(*) * Column1 AS SumColumn1
    FROM dbo.table_name
    WHERE Column2 = '1'
    GROUP BY Column1
    ORDER BY Column1

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply