SQL "CASE" Help

  • i have a query with the following case statement.

    SUM(CASE WHEN Department = 'SOUH' THEN TrxFullPrice

    ELSE 0

    END) AS SOUH

    i need to add multiple departments however i cannot get it to work.

    something like this.

    SUM(CASE WHEN Department = 'SOUH', 'DIAM', 'JEWR' THEN TrxFullPrice

    ELSE 0

    END) AS SOUH

    can someone please let me know if this is possible?

    or provide a workaround?

  • Is this what you require?

    CREATE TABLE #T(Department VARCHAR(20),TrxFullPrice MONEY)

    INSERT INTO #T

    SELECT 'Souh',100.00 UNION ALL

    SELECT 'Diam',110.00 UNION ALL

    SELECT 'Jewr',120.00 UNION ALL

    SELECT 'Nuts',500

    SELECT SUM(TrxFullPrice) FROM #T

    WHERE Department IN('SOUH', 'DIAM', 'JEWR')

    Result is $330.00

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • No this will not work.

    maybe doing a union on separate case statements?

  • Thank you for the person who gave me the answer;

    SUM(CASE WHEN Department in ('SOUH', 'DIAM', 'JEWR') THEN TrxFullPrice

    ELSE 0

    END) AS SOUH

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

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