运行 STRING_AGG 函数时查找错误

Finding Error when running STRING_AGG function

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

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

但我不能 运行 因为它给了我一个错误

An aggregate function that has both DISTINCT and ORDER BY arguments can only ORDER BY expressions that are arguments to the function

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

您想要按时间戳排序的不同状态吗? 如果是这样,您可以先按 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