我有一张销售表,包括购买/退货/换货示例:
--------**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
标准是:在 2012 年 11 月 5 日至 2012 年 11 月 20 日期间找到每个客户的总数。如果客户交换了在此期间购买的商品并且在同一天购买的商品将被计算在内。预期结果是:
CustID | DOP | Price
222 | 11/20/12 | 45.99
111 | 12/01/12 | 70.99
我试图得到总数,但当然这是不对的:
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]
请帮忙!谢谢大家。