gpt4 book ai didi

google-bigquery - BigQuery 物化 View - 组中最后一个

转载 作者:行者123 更新时间:2023-12-04 01:17:55 24 4
gpt4 key购买 nike

在 BigQuery 中是否可以为基表中的每个组创建一个包含最新行的物化 View 。
例如

CREATE TABLE basetable (
group_id INT64, timestamp TIMESTAMP, value FLOAT64
);

INSERT INTO basetable (group_id, timestamp, value) VALUES
(1, '2020-01-01', 0.1),
(1, '2020-01-02', 0.2),
(2, '2020-01-02', 0.1),
(2, '2020-01-01', 0.2);
Base table
+----------+--------------+-------+
| group_id | timestamp | value |
+----------+--------------+-------+
| 1 | '2020-01-01' | 0.1 |
| 1 | '2020-01-02' | 0.2 |
| 2 | '2020-01-02' | 0.1 |
| 2 | '2020-01-01 | 0.2 |
+----------+--------------+-------+
我希望物化 View 如下所示
Materialized view 
+----------+--------------+-------+
| group_id | timestamp | value |
+----------+--------------+-------+
| 1 | '2020-01-02' | 0.2 |
| 2 | '2020-01-02' | 0.1 |
+----------+--------------+-------+
BigQuery 物化 View 不支持分析函数或联接。
有没有其他方法可以创建这样的 View ?

最佳答案

您最多可以这样做,请注意结果是一个包含一项的数组

CREATE MATERIALIZED VIEW  name as
SELECT group_id,
max(t.timestamp) as timestamp,
ARRAY_AGG(t.value IGNORE NULLS ORDER BY t.timestamp DESC LIMIT 1) as value
FROM table t
group by group_id
那么你也需要一个 View
create view viewname as
SELECT group_id,timestamp
cast(value [safe_offset(0)] as string) as value
FROM materialized_view

关于google-bigquery - BigQuery 物化 View - 组中最后一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63028965/

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