计算时间戳的差异并跳过 NULL

Calculate Difference of Timestamps and skip NULL

如何跳过第一行来计算差异的平均值? “WHERE”筛选器不起作用 (错误:WHERE 子句中不允许解析函数)

  1. 为什么我可以跳过“557186”的第一个值?
  2. 是否可以声明“Unit”以便稍后将此变量用于“WHERE”?
SELECT
  FORMAT_TIMESTAMP("%F", Timestamp) AS Date,
  FORMAT_TIMESTAMP("%R", Timestamp) AS Unit,
  LAG(FORMAT_TIMESTAMP("%R", Timestamp)) OVER (ORDER BY FORMAT_TIMESTAMP("%R", Timestamp)) AS CycleTimePreValue,
  CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)-LAG(CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)) OVER (ORDER BY CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)) AS CycleTimePreValueSeconds,
FROM
  `wh-lr-sk.Quality.Retouren_IMPORT`
  # WHERE (CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)-LAG(CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)) OVER (ORDER BY CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64))) < 1000
ORDER BY
  Unit
LIMIT
  10

要“声明”单元,您可以使用 CTE(常用 table 表达式)。

with time_lags as (
SELECT
  FORMAT_TIMESTAMP("%F", Timestamp) AS Date,
  FORMAT_TIMESTAMP("%R", Timestamp) AS Unit,
  LAG(FORMAT_TIMESTAMP("%R", Timestamp)) OVER (ORDER BY FORMAT_TIMESTAMP("%R", Timestamp)) AS CycleTimePreValue,
  CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)-LAG(CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)) OVER (ORDER BY CAST(FORMAT_TIMESTAMP("%s", Timestamp) AS INT64)) AS CycleTimePreValueSeconds,
FROM
  `wh-lr-sk.Quality.Retouren_IMPORT`
)
SELECT
    *
FROM time_lags
WHERE Unit < 1000
AND CycleTimePreValue IS NOT NULL
ORDER BY
  Unit
LIMIT
  10

要删除我刚刚添加的空值,请将 AND CycleTimePreValue IS NOT NULL 添加到 where 子句。