gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:29:47 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com