【问题标题】:Finding Error when running STRING_AGG function运行 STRING_AGG 函数时发现错误
【发布时间】:2020-08-10 13:35:14
【问题描述】:

我想询问有关 BigQuery 中的脚本的问题。所以,我尝试使用下面的查询

SELECT id, STRING_AGG(DISTINCT status, ', ' ORDER BY timestamp) AS grouping
FROM table
GROUP BY id

但我无法运行它,因为它给了我一个错误

同时具有 DISTINCT 和 ORDER BY 参数的聚合函数只能 ORDER BY 表达式作为函数的参数

谁能帮我解决这个错误?提前谢谢!

【问题讨论】:

  • 当您订购 status 的不同值时 - 您要使用具有该状态的多行中的哪个 timestamp?这就是为什么STRING_AGG 不支持它,因为它只是不知道如何订购。因此,您需要明确您的要求/期望,以便我们为您提供帮助

标签: google-bigquery string-aggregation


【解决方案1】:

您想要按时间戳排序的不同状态吗? 如果是这样,您可以先按timestamp 为每个idstatus 排序,然后聚合。

WITH ordered as (
    SELECT id, status
    FROM table
    ORDER BY id, row_number() over (partition by id ORDER BY timestamp)
)
SELECT id, STRING_AGG(DISTINCT status, ', ') AS grouping
FROM ordered
GROUP BY id

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多