[英]Resetting row_number based on 2 rows
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.如果当前行和上一行的日期列之间的时间少于 12 个月,我想为每一行分配行号。 If there are more than 12 months, I want to restart the row number.
如果超过12个月,我想重新开始排号。 I also want to do with the customer ID, product and region partitions.
我还想处理客户 ID、产品和区域分区。
So, the row number column for the above table needs to be 1,2,1,1,2
.因此,上表的行号列需要为
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.第 3 行需要重新开始,因为对于相同的客户、产品和区域,2015-09-02 和 2019-09-02 之间有超过 1 年的时间。
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查询结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.