根据行号分区更新 bigquery 值

Update bigquery value based on partition by row number

我有一个 table,其中我有错误日期的记录。我想将它们更新为“snapshot_date”的前一天。我已将查询写入 select 我想为其更新日期的值,但我不知道如何编写更新查询以将其更改为前一天。

见截图

查询 select 条有问题的记录

Select * FROM(
    SELECT 
        *,
        ROW_NUMBER() OVER(PARTITION BY Period, User_Struct) rn 
     FROM `XXX.YYY.TABLE` 
     where Snapshot_Date = '2021-10-04'
     order by Period, User_Struct, Num_Active_Users asc
) where rn = 1

使用 DATE_SUB 你可能会得到前一天,即

SELECT DATE_SUB(cast('2021-10-04' as DATE), interval '1' day)

会给2021-10-03.

您可以使用 Big Query Update Statement Syntax

尝试以下操作
UPDATE 
    `XXX.YYY.TABLE` t0
SET
    t0.Snapshot_Date = DATE_SUB(t2.Snapshot_Date, interval '1' day)
FROM (
    SELECT * FROM(
        SELECT 
            *,
            ROW_NUMBER() OVER(PARTITION BY Period, User_Struct) rn 
        FROM 
            `XXX.YYY.TABLE` 
        WHERE
             Snapshot_Date = '2021-10-04'
        ORDER BY -- recommend removing order by here and use recommendation below for row_number
             Period, User_Struct, Num_Active_Users asc
    ) t1 
    WHERE rn = 1
)  t2
WHERE
   t0.Snapshot_Date = t2.Snapshot_Date AND -- include other columns to match/join subquery with main table on

您还应指定在使用 ROW_NUMBER 时应如何对行进行排序,例如

ROW_NUMBER() OVER (PARTITION BY Period, User_Struct ORDER BY Num_Active_Users asc) 

如果这会生成 same/desired 个结果。

让我知道这是否适合你。