There is a table like:
Customer ID | Product | Region | date |
---|---|---|---|
1 | A | US | 2015-08-01 |
1 | A | US | 2015-09-02 |
1 | A | US | 2019-09-02 |
2 | B | UK | 2018-10-02 |
2 | B | UK | 2019-09-02 |
I want to assign row numbers to each row if there are less than 12 months between the date columns of the current and previous row. If there are more than 12 months, I want to restart the row number. I also want to do with the customer ID, product and region partitions.
So, the row number column for the above table needs to be 1,2,1,1,2
. The 3rd row needs to restart because for the same customer, product and region, there is more than 1 year between 2015-09-02 and 2019-09-02.
I know that to assign row numbers without the condition there is the below code but don't know how to insert a condition:
ROW_NUMBER() OVER (PARTITION BY customer_id,product,region ORDER BY date)
You might consider below approach.
SELECT * EXCEPT(flag, part), ROW_NUMBER() OVER w2 AS rn FROM (
SELECT *, COUNTIF(flag) over w1 AS part FROM (
SELECT *, IFNULL(DATE_DIFF(date, LAG(date) OVER w0, DAY) > 365, false) AS flag
FROM sample_table
WINDOW w0 AS (PARTITION BY customer_id, product, region ORDER BY date)
) WINDOW w1 AS (PARTITION BY customer_id, product, region ORDER BY date)
) WINDOW w2 AS (PARTITION BY customer_id, product, region, part ORDER BY date);
Query results