Following is my Raw data
DATE Product_id Customer_ID
----------------------------
01-JUL-14 60 A
01-AUG-14 45 A
01-SEP-14 45 A
01-SEP-14 50 A
01-OCT-14 30 A
01-JUL-14 60 B
01-AUG-14 45 B
01-SEP-14 45 B
01-OCT-14 30 B
This is the desired result for my rolling counts
MMYY Distinct Customer
Product
-------------------------
JUL-14 1 A
AUG-14 2 A
SEP-14 3 A
OCT-14 3 A
JUL-14 1 B
AUG-14 2 B
SEP-14 2 B
OCT-14 2 B
The way I need it to work is that for each MMYY, I need to look back 3 months, and COUNT DISTINCT Products per CUSTOMER Products can be repeat. And also a customer can have more than 1 product in same month.
Normally I would write the query like so
SELECT
customer_ID,
T.Date as MMYY,
COUNT(DISTINCT Product_id)
OVER (PARTITION BY customer_ID ORDER BY T.Date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
AS Last_3_month_count
FROM T
But the problem is that we cannot use COUNT(DISTINCT Product_id) its gives an error. Suggest me other way to do solve this problem.