[英]Create a new column containing the concatenated unique elements of another in BigQuery SQL, using `string_agg`
Given the following data:给定以下数据:
WITH abc AS
(
SELECT 1234 id, 'Aug' month, 'P' value UNION ALL
SELECT 1234, 'Sept', 'P' UNION ALL
SELECT 4567, 'Sept', 'D'
)
SELECT
month,
value,
FROM abc
Which looks as:看起来像:
I'd like to create a new column values
which contains all values from the value
columns.我想创建一个新列
values
,其中包含value
列中的所有值。
So for this example data the new column would contain P, D
.因此,对于此示例数据,新列将包含
P, D
。
Having done this the output would be (I've just written this out in Excel):完成此操作后,output 将是(我刚刚用 Excel 将其写出来):
I tried to do this as follows:我尝试按如下方式执行此操作:
WITH abc AS
(
SELECT 1234 id, 'Aug' month, 'P' value
UNION ALL
SELECT 1234, 'Sept', 'P'
UNION ALL
SELECT 4567, 'Sept', 'D'
)
SELECT
month,
value,
STRING_AGG(distinct value, " & ") as str_agg
FROM abc
But I get this error:但我收到此错误:
SELECT list expression references column month which is neither grouped nor aggregated at [7:5]
SELECT 列表表达式引用列月份,它既没有分组也没有聚合在 [7:5]
I'm not sure how to aggregate/group the value
column in order to satisfy this error.我不确定如何聚合/分组
value
列以满足此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.