google-bigquery - 如何在 BigQuery 中执行 Pandas 列转换?

标签 google-bigquery

假设我们有一个包含以下数据的数据集:

timestamp,col1

1533286270,1
1533286271,2
1533286272,3
1533286273,4
1533286274,5

我想得到前面的 col1 col1_prev 中的值能够比较它们。结果应该与 pandas.shift(-1) 相同.

如何使用纯 SQL 查询实现此功能?

查询结果应如下所示:
timestamp,col1,col1_prev

1533286270,1,NULL
1533286271,2,1
1533286272,3,2
1533286273,4,3
1533286274,5,4

最佳答案

使用 lag()函数是一种方式:

WITH
  input AS (
  SELECT
    1533286270 AS timestamp,
    1 AS col1
  UNION ALL
  SELECT
    1533286271 AS timestamp,
    2 AS col1
  UNION ALL
  SELECT
    1533286272 AS timestamp,
    3 AS col1
  UNION ALL
  SELECT
    1533286273 AS timestamp,
    4 AS col1
  UNION ALL
  SELECT
    1533286274 AS timestamp,
    5 AS col1 )
SELECT
  timestamp,
  col1,
  LAG(col1) OVER(ORDER BY col1) AS col1_prev
FROM
  input

enter image description here

关于google-bigquery - 如何在 BigQuery 中执行 Pandas 列转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669039/

相关文章:

google-bigquery - Google Big Query 错误 : CSV table encountered too many errors, 放弃。行:1 错误:1

google-bigquery - bigquery steaming buffer持续多久

google-bigquery - Avro 纪元日期时间到 bq 时间戳

javascript - Google Cloud 函数错误 - PubSub 到 BigQuery

java - 如何使 java 准备语句像大查询的查询或如何在大查询中转义参数

encryption - 用户如何证明特定帐户有权访问 BigQuery? (AEAD 加密和授权 View )

python - BigQuery 检查数组重叠

google-cloud-platform - 在没有表行更新/上传发生时设置大查询警报

python - 从 gcloud ml-engine 作业访问 Big Query

google-bigquery - 将数据流式传输到 Bigquery 与将数据上传到 PubSub 然后使用数据流将数据插入到 Bigquery 之间的优缺点是什么