Sum subset in group

  • I'm not sure how to do this:

    column1 column2

    1 5

    2 7

    1 1

    Total (I want the total of column2 where column1 = 1)

    I'm sure there's a way to do it?

    Anyone?

  • SELECT column1, sum (column2)

    FROM <yourtable>

    WHERE column1=1

    GROUP BY column1

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • For SSRS, it should be something like this:

    =Sum(IIF(Fields!column1.Value = 1, column2, 0))

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Alan.B (10/10/2014)


    SELECT column1, sum (column2)

    FROM <yourtable>

    WHERE column1=1

    GROUP BY column1

    How embarrassing, I did not realize, at the time I posted this, that this was an SSRS forum (smacking forehead).

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

Viewing 4 posts - 1 through 4 (of 4 total)

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