Salesforce 营销云 SQL PIVOT

Salesforce Marketing Cloud SQL PIVOT

我正在尝试在 SFMC 中创建一个数据透视表以将多行合并为一列。查询 运行 成功,但未正确填充。我的 table 有 2 列:电子邮件地址和 product_sku。每个电子邮件地址在 table 中出现 3 次,旁边有一个不同的 product_sku。我希望输出具有唯一的电子邮件地址,其中 3 product_skus

示例文件:

Email | product_sku
test@test.com | 12314321131412
test@test.com | 45353643645
test@test.com | 0953242425352

示例所需输出:

email | product_sku_header_1 | product_sku_header_2 | product_sku_header_3
---------------------------------------------------------------------------------
test@test.com | 12314321131412 | 45353643645 | 0953242425352

我的查询是:

select
email
, sum(case when product_sku ='%' then 1 else 0 end) as product_sku_header_1
, sum(case when product_sku ='%' then 1 else 0 end) as product_sku_header_2
, sum(case when product_sku ='%' then 1 else 0 end) as product_sku_header_3
FROM data_extension
group by 
email

提前致谢!

您可以使用 row_number() 进行条件聚合:

select email,
       max(case when seqnm = 1 then product_sku end) as  product_sku_header_1,
       max(case when seqnm = 2 then product_sku end) as  product_sku_header_2,
       max(case when seqnm = 3 then product_sku end) as  product_sku_header_3
from (select de.*,
             row_number() over (partition by email order by product_sku) as seqnum
      from data_extension de
     ) de
group by email;