繁体   English   中英

BigQuery LAG 仅返回空值

[英]BigQuery LAG returning only nulls

我在 bigquery 中遇到问题,我没有使用 LAG function 获得所需的 output:

WITH
  base AS (
    SELECT "2022-11-01" month , 1100 icount union all
    SELECT "2022-10-01" month , 1000 icount union all
    SELECT "2022-09-01" month , 900 icount union all
    SELECT "2022-08-01" month , 800 icount union all
    SELECT "2022-07-01" month , 700 icount union all
    SELECT "2022-06-01" month , 600 icount union all
    SELECT "2022-05-01" month , 500 icount union all
    SELECT "2022-04-01" month , 400 icount
  )
SELECT
  month,
  icount,
  LAG(icount) OVER w1 AS previous_icount
FROM base
WINDOW w1 AS (
  PARTITION BY month ORDER BY icount)
ORDER BY
  month DESC

结果是:

我算 上一个_icount
2022-11-01 1100 null
2022-10-01 1000 null
2022-09-01 900 null
2022-08-01 800 null
2022-07-01 700 null
2022-06-01 600 null
2022-05-01 500 null
2022-04-01 400 null

但我期待得到以下结果:

我算 上一个_icount
2022-11-01 1100 1000
2022-10-01 1000 900
2022-09-01 900 800
2022-08-01 800 700
2022-07-01 700 600
2022-06-01 600 500
2022-05-01 500 400
2022-04-01 400 null

我浏览了文档,但无法弄清楚我错过了什么才能做到这一点。

正如@Jaytiger 所指出的,问题在于您的 window 定义。

month分区时,您对由唯一月份标识的每组行应用处理。

由于表中的month只有一行,因此LAG function 应用于仅包含一个值的分区,对应的月份。 本例中的LAG ged 值为NULL

你想要做的是通过将不同记录保存在同一个分区中的东西来PARTITION你的表:

  • 如果您有一个公共列(例如: shop ),您将按此列进行分区,
  • 在这里你没有这样的列所以你可以PARTITION BY 1
  • 但实际上你甚至不必PARTITION ,BigQuery 会明白只有一个分区需要考虑:
WITH
  base AS (
    SELECT "2022-11-01" month , 1100 icount union all
    SELECT "2022-10-01" month , 1000 icount union all
    SELECT "2022-09-01" month , 900 icount union all
    SELECT "2022-08-01" month , 800 icount union all
    SELECT "2022-07-01" month , 700 icount union all
    SELECT "2022-06-01" month , 600 icount union all
    SELECT "2022-05-01" month , 500 icount union all
    SELECT "2022-04-01" month , 400 icount
  )
SELECT
  month,
  icount,
  LAG(icount) OVER w1 AS previous_icount
FROM base
WINDOW w1 AS (
  PARTITION BY 1 -- unnecessary 
ORDER BY icount)
ORDER BY
  month DESC

这给出了期望的结果

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM