将相关列连接到另一个 table 的另一列(使用公共键)

Concatenate a related column to another column of another table (with a common key)

我正在寻找 BigQuery 的专业知识,

我有两个 table(t1 和 t2)与一个公共字段(sku t1 和 prod t2)相关

我需要连接一个基于 sku t1 的列 (str t2),但我没能做到。

我尝试了以下方法,

SELECT t1.SKU, CONCAT(t2.STR,',') 作为负载 从 table t1 LEFT JOIN table t2 ON t1.SKU = CAST(t2.PROD AS STRING)

这是 table 1 (SKU)

SKU NAME
SKU1 NAMEPRODUCT1
SKU2 NAMEPRODUCT2

这是 table 2 (PROD)

PROD STR
SKU1 99
SKU1 549
SKU2 25
SKU2 98

输出应该如下,

SKU LOAD
SKU1 99,549,1,97
SKU2 25,98,2,88

感谢您的反馈!

你快到了! 您要查找的是 STRING_AGG.
尝试以下操作:

select
    t1.SKU
    , string_agg(cast(str as string),',')
from table_1 t1
left join table_2 t2
 on t1.SKU=cast(t2.prod as string)
group by t1.SKU

请注意,我假设 str 列是一个 INTEGER,这就是为什么要进行强制转换的原因,并且我假设它不是一个字符串,我维护了您对 t2.prod 列的强制转换这就是为什么你需要把它作为一个。

有关 STRING_AGG 的更多信息,请参阅此处的文档: https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#string_agg