• There's an easier way and it's explained in this article: http://www.sqlservercentral.com/articles/T-SQL/63681/

    This is an example that might not work because I have nothing to test on.

    -- Daily Report

    SELECT UserID,

    SUM(CASE WHEN OrderStatusID = 2 THEN OrderCount END) As Status2Count,

    SUM(CASE WHEN OrderStatusID = 3 THEN OrderCount END) As Status2Count,

    SUM(CASE WHEN OrderStatusID = 4 THEN OrderCount END) As Status2Count,

    SUM(CASE WHEN OrderStatusID = 8 THEN OrderCount END) As Status2Count,

    FROM(

    SELECT u.UserID,

    o.OrderStatusID,

    COUNT(*) OrderCount

    FROM Users u

    JOIN Orders o ON u.UserID = o.userID

    WHERE OrderStatusID IN( 2, 3, 4, 8)

    GROUP BY u.UserID, OrderStatusID

    HAVING COUNT(*) > 0)x --This is a pre-aggregation to improve performance, but should be tested.

    GROUP BY UserID

    EDIT: I was evaluating wy word choice and easier might not be accurate when there's no practice on how to do it, but it's certainly more efficient and will come natural when you fully understand it.

    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