在BigQuery中保留值的最佳方法是什么?例如,如果单元格中的值是,(none)
那么它应该获得不是的最后一个已知值,(none)
但是如果该值随后发生变化,则开始保留该值。请参阅以下示例,了解我想要的内容。我尝试使用LAG函数,但仅适用于一行。
Row Value Persisted
1 Apple Apple
2 (none) Apple
3 (none) Apple
4 (none) Apple
5 Orange Orange
6 (none) Orange
以下是BigQuery标准SQL
为了实现您的目标,您需要在数据中添加一些额外的字段来定义值的顺序-通常是带有时间戳,日期等的列。数据类型或数字反映位置。在下面的示例中,我将其ts
用作此类列
#standardSQL
SELECT ts, value,
LAST_VALUE(IF(value = '(none)', NULL, value) IGNORE NULLS) OVER(ORDER BY ts) Persisted
FROM `project.dataset.table`
您可以使用问题中的示例数据来测试,玩游戏,如以下示例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 ts, 'Apple' Value UNION ALL
SELECT 2, '(none)' UNION ALL
SELECT 3, '(none)' UNION ALL
SELECT 4, '(none)' UNION ALL
SELECT 5, 'Orange' UNION ALL
SELECT 6, '(none)'
)
SELECT ts, value,
LAST_VALUE(IF(value = '(none)', NULL, value) IGNORE NULLS) OVER(ORDER BY ts) Persisted
FROM `project.dataset.table`
带输出
Row ts value Persisted
1 1 Apple Apple
2 2 (none) Apple
3 3 (none) Apple
4 4 (none) Apple
5 5 Orange Orange
6 6 (none) Orange
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句