• Sean Lange (9/12/2013)


    select SUM(Case when MyType = 2 then -1 * MyHours else MyHours end) as TotalHours

    from #SomeTable

    If I understand the original OP correctly (" If something has been credited, the billed item row should be excluded") this would not provide the right answer with this sample data:

    INSERT INTO #SomeTable

    SELECT 'xxx',3,1 UNION ALL

    SELECT 'yyy',3,2 UNION ALL

    SELECT 'xxx',4,1 UNION ALL

    SELECT 'xxx',1,1;

    I believe the right answer would be 8, your query would return a 5.

    ... but who knows without any ddl :Whistling:

    "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