I have a sale table includes purchases/returns/exchanges
Sample:
--------**saleTbl**------------
CustID | DOP | SKU | Price
111 | 11/05/12 | 001 | 45.99
222 | 11/20/12 | 001 | 45.99
111 | 11/06/12 | 002 | 40.95
111 | 11/06/12 | 001 | -45.99
111 | 11/19/12 | 004 | 50.00
222 | 11/25/12 | 003 | 20.99
111 | 12/01/12 | 002 | -40.95
111 | 12/01/12 | 003 | 20.99
Criteria is: find total for each customer during 11/05/12 – 11/20/12. If customer exchanged the item that was purchased during that time and purchase with the same day will be count.
The expected result is:
CustID | DOP | Price
222 | 11/20/12 | 45.99
111 | 12/01/12 | 70.99
I have tried to get the total but of course it is not right:
SELECT DISTINCT [num_cp] AS 'Member Id'
,MAX([dop]) AS 'Date'
,SUM([price]) AS 'Point'
FROM [Mailing_List].[dbo].[UGG_DoublePoint]
WHERE [num_cp] IN
(
SELECT [num_cp]
FROM [Mailing_List].[dbo].[UGG_DoublePoint]
GROUP BY [num_cp]
HAVING SUM([price]) >0
)
--AND
AND [dop] BETWEEN '11/05/12' AND '11/20/12'
GROUP BY [num_cp]
Please help! Thanks everyone.
I think you need to change your query to this
Check SQL Fiddle Demo