• lanky_doodle (9/13/2013)


    Alan.B (9/12/2013)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:

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

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

    The billed item row and the credit row should both be excluded. So say there was only 2 rows, both for the same case, one row had 2 hours billed and the other row 2 hours credited, the result should be 0.

    In otherwords, if something has been credited, the results should show as if it was never billed in the first place. We're trying to work out sales money, so if someone bills £200 and then credits it all back, they've really not billed (gained) anything.

    Then my solution should work just fine?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/