Lets say I have a table of purchases that looks like this:
Purchases
---------
Item_ID
Purchase_Date
Customer_ID
How would I be able to obtain the set of purchases, starting from the first purchase, for each customer that are at least X days after the last purcahse? For example, if we have the data below, and x=10:
Item_ID PurchaseDate Customer_ID
123 07/29/23 1000
123 08/04/23 1000
123 08/16/23 1000
563 07/03/23 7785
563 07/05/23 7785
788 08/17/23 2489
The query should return:
Item_ID PurchaseDate Customer_ID
123 07/29/23 1000
123 08/11/23 1000
563 07/03/23 7785
788 08/17/23 2489
So, for customer 1000, the first purchase counts, because its a new purchase. The second purchase is less than 10 days from the last purchase so it's discarded. The third purchase for this customer counts because it is more than or equal to 10 days after the last purchase. For customer 7785, only the first counts because the second purcahse is less than 10 days after the last purcahse, and finally for customer 2489, it only has one new purchase so that just counts as the new purchase.(note the first purchase always counts)
I was thinking along the lines of using the lag function
SELECT
t.Item_ID,
t.PurchaseDate,
t.Customer_ID
FROM (
SELECT
p.Item_ID,
p.PurchaseDate,
p.Customer_ID,
LAG OVER(PARTITION BY Customer_ID ORDER BY PurchaseDate) next_purchase
ROW_NUMBER() OVER(PARTITION BY Customer_ID ORDER BY PurchaseDate) rownum
FROM
PurchaseTable p
) t
WHERE
DATEDIFF(DAY, t.PurchaseDate, t.next) >= 10) or rownum=1
EDIT: Apologies, I had the description incorrect. I changed the question and example to the correct details.