简体   繁体   English

根据 2 行重置 row_number

[英]Resetting row_number based on 2 rows

There is a table like:有一张表,如:

Customer ID客户ID Product产品 Region地区 date日期
1 1个 A一种 US我们 2015-08-01 2015-08-01
1 1个 A一种 US我们 2015-09-02 2015-09-02
1 1个 A一种 US我们 2019-09-02 2019-09-02
2 2个 B UK英国 2018-10-02 2018-10-02
2 2个 B UK英国 2019-09-02 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.

相关问题 ROW_NUMBER() 与条件 BIGQUERY - ROW_NUMBER() WITH CONDITION BIGQUERY BigQuery“薛定谔行”或为什么 ROW_NUMBER() 不是一个好的标识符 - BigQuery "Schrödingers Row" or why ROW_NUMBER() is not a good identifier 如何在不使用多个连接的情况下使用 Row_number function 连接单个单元格中的不同行 - How Can I Concatenate Different Rows In a Single Cell Using Row_number function Without Using Multiple Joins 如何将 row_number 分配给 SQL 中的缺失值 - How to assign row_number to missing values in SQL Biguqery Row Number Perioding 基于特定值 - Biguqery Row Number Perioding based on specific values 在大表上使用 ROW_NUMBER() 时出现 Google Bigquery Memory 错误 - 用短唯一标识符替换长 hash 的方法 - Google Bigquery Memory error when using ROW_NUMBER() on large table - ways to replace long hash by short unique identifier 根据行号执行 SQL 更新并使用上一行进行计算 - Performing SQL updates based on row number and using previous row for calculations SQL - 如何 select x 特定行之前的行数 - SQL - How to select x number of rows prior to a specific row 如何根据组数而不是行数限制表结果? - SQL - How to limit table results based on number of groups instead of number of rows? - SQL AWS 根据文件中存在的行数将 S3 中的大文件分成小文件块 - AWS Breaking a large file in S3 into small chucks of files based on number of rows present in the file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM