You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于其他列值合并行值的Presto SQL查询方案求助

Presto SQL实现按客户ID合并物业ID列数据

基础数据表

客户ID物业ID客户预订数客户取消数
A101
B2101
C31001
C41001
D5201

当前使用的SQL查询

select customer_id, property_id, bookings_per_customer, cancellations_per_customer
from table

期望合并结果

客户ID物业ID客户预订数客户取消数
A101
B2101
C3 , 41001
D5201

Presto SQL解决方案

在Presto中可以用string_agg()函数拼接同一客户的多个物业ID,同时按客户ID、预订数、取消数分组(这些字段对同一客户取值一致),具体语句如下:

select 
    customer_id,
    string_agg(property_id, ' , ') as property_id,
    bookings_per_customer,
    cancellations_per_customer
from table
group by customer_id, bookings_per_customer, cancellations_per_customer

说明

  • string_agg(字段名, 分隔符):将分组内指定字段的所有值用指定分隔符拼接成字符串,这里用' , '匹配期望结果的格式。
  • group by子句需包含所有非聚合字段,确保同一客户的相同统计值会被合并。

内容的提问来源于stack exchange,提问作者bobbytici

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 18:15:40