gpt4 book ai didi

sql - bigquery order by date 在分析函数中不起作用?

转载 作者:行者123 更新时间:2023-12-02 17:09:34 30 4
gpt4 key购买 nike

这是示例数据

    WITH dummy_data AS (
SELECT DATE '2017-01-01' AS ref_month, 18 AS value, 1 AS id
UNION ALL SELECT DATE '2017-02-01' AS ref_month, 20 AS value, 1 AS id
UNION ALL SELECT DATE '2017-03-01' AS ref_month, 22 AS value, 1 AS id
-- UNION ALL SELECT DATE '2017-04-01' as ref_month, 28 as value, 1 as id
UNION ALL SELECT DATE '2017-05-01' AS ref_month, 30 AS value, 1 AS id
UNION ALL SELECT DATE '2017-06-01' AS ref_month, 37 AS value, 1 AS id
UNION ALL SELECT DATE '2017-07-01' AS ref_month, 42 AS value, 1 AS id
-- UNION ALL SELECT DATE '2017-08-01' as ref_month, 55 as value, 1 as id
-- UNION ALL SELECT DATE '2017-09-01' as ref_month, 49 as value, 1 as id
UNION ALL SELECT DATE '2017-10-01' AS ref_month, 51 AS value, 1 AS id
UNION ALL SELECT DATE '2017-11-01' AS ref_month, 57 AS value, 1 AS id
UNION ALL SELECT DATE '2017-12-01' AS ref_month, 56 AS value, 1 AS id
UNION ALL SELECT DATE '2017-01-01' AS ref_month, 18 AS value, 2 AS id
UNION ALL SELECT DATE '2017-02-01' AS ref_month, 20 AS value, 2 AS id
UNION ALL SELECT DATE '2017-03-01' AS ref_month, 22 AS value, 2 AS id
UNION ALL SELECT DATE '2017-04-01' AS ref_month, 28 AS value, 2 AS id
-- UNION ALL SELECT DATE '2017-05-01' as ref_month, 30 as value, 2 as id
-- UNION ALL SELECT DATE '2017-06-01' as ref_month, 37 as value, 2 as id
UNION ALL SELECT DATE '2017-07-01' AS ref_month, 42 AS value, 2 AS id
UNION ALL SELECT DATE '2017-08-01' AS ref_month, 55 AS value, 2 AS id
-- UNION ALL SELECT DATE '2017-09-01' AS ref_month, 49 AS value, 2 AS id
-- UNION ALL SELECT DATE '2017-10-01' as ref_month, 51 as value, 2 as id
UNION ALL SELECT DATE '2017-11-01' AS ref_month, 57 AS value, 2 AS id
UNION ALL SELECT DATE '2017-12-01' AS ref_month, 56 AS value, 2 AS id
)

我正在尝试运行这个简单的查询

select
id
,value
, ref_month
, ARRAY_AGG(value) OVER w1 as agg_last_3_values
from dummy_data
window w1 as (partition by id order by ref_month RANGE BETWEEN 2 PRECEDING AND CURRENT ROW)

为什么会出现以下错误?

ORDER BY key must be numeric in a RANGE-based window with OFFSET PRECEDING or OFFSET FOLLOWING boundaries, but has type DATE

我不明白为什么它不能处理日期....有什么建议吗?

最佳答案

使用rows代替range:

select id, value, ref_month, ARRAY_AGG(value) OVER w1 as agg_last_3_values
from dummy_data
window w1 as (partition by id order by ref_month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)

range 很棘手,因为它必须处理关系——因此具有相同排序键值的行将包含在窗口中。这通常会导致难以调试的错误,但偶尔会有用。

我不熟悉其他语言对 rangeorder by 的限制。但是,BigQuery 似乎假设 order by 键是数字。

关于sql - bigquery order by date 在分析函数中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49923601/

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