Basically, I am attempting to combine 2 queries into one. To simplify it, I have a customer table, a Sales table, and a Refund table (whose structure matches the Sales table). I need to report an aggregate by month, returning the following format:
Month Cust ID Cust Name Sales Refunds
------------------------------------------------
Jan 100 Bob's bobbers 50 -30
Feb 100 Bob's bobbers 30 -20
Mar 100 Bob's bobbers 150 -60
...
The query, simplified, I'm using is:
SELECT S.Date, C.Cust ID, C.Cust Name, Sum(S.Sales), Sum(R.Refunds)
FROM Customer AS C
INNER JOIN Sales AS S ON C.Cust ID = S.Cust ID
INNER JOIN Returns AS R ON C.Cust ID = R.Cust ID
GROUP BY S.Date, C.Cust ID, C.Cust Name
HAVING S.Date >= '01/01/2010' AND C.Cust ID = '100'
What I'm getting back is:
Month Cust ID Cust Name Sales Refunds
------------------------------------------------
Jan 100 Bob's bobbers 350 -550
Feb 100 Bob's bobbers 210 -330
Mar 100 Bob's bobbers 1050 -550
...
Again, this is all simplified, but obviously if the simplified version isn't returning the correct values then the full query won't either..
Thanks for the help!
Month Cust ID Cust Name Sales Refunds
------------------------------------------------
Jan 100 Bob's bobbers 50 -30
Feb 100 Bob's bobbers 30 -20
Mar 100 Bob's bobbers 150 -60
...
The query, simplified, I'm using is:
SELECT S.Date, C.Cust ID, C.Cust Name, Sum(S.Sales), Sum(R.Refunds)
FROM Customer AS C
INNER JOIN Sales AS S ON C.Cust ID = S.Cust ID
INNER JOIN Returns AS R ON C.Cust ID = R.Cust ID
GROUP BY S.Date, C.Cust ID, C.Cust Name
HAVING S.Date >= '01/01/2010' AND C.Cust ID = '100'
What I'm getting back is:
Month Cust ID Cust Name Sales Refunds
------------------------------------------------
Jan 100 Bob's bobbers 350 -550
Feb 100 Bob's bobbers 210 -330
Mar 100 Bob's bobbers 1050 -550
...
Again, this is all simplified, but obviously if the simplified version isn't returning the correct values then the full query won't either..
Thanks for the help!