oracle 聚合函数 结果集为一对多,将多个行合并,并用逗号分隔

select t1.cust_name 客户名称,
t3.offer_name 套餐名称,
t4.os_status  使用状态,
t5.acct_name 账户名称
from cm_cust  t1 
join ur_user   t2
on t1.cust_id = t2.cust_id
join ur_user_offer  t3
on t3.user_id = t2.user_id
join ur_user_status t4
on t4.user_id = t2.user_id
join ac_acct t5
on t5.cust_id = t1.cust_id
where t1.state = 'Y'
and t2.user_state = '1'

sql 的结果集为一对多的关系,想要将套餐名称,使用状态合并,并且用逗号分隔

可以使用LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)函数

示例代码:

select t1.cust_name 客户名称,
listagg (t3.offer_name,',') within group (order by t3.offer_name) 套餐名称
 ,listagg (t4.os_status,',')within group (order by t4.os_status) 使用状态
from cm_cust  t1 
join ur_user   t2
on t1.cust_id = t2.cust_id
join ur_user_offer  t3  
on t3.user_id = t2.user_id
join ur_user_status t4
on t4.user_id = t2.user_id
where t1.state = 'Y'
and t2.user_state = '1'
group by t1.cust_name

运行结果

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 19:32:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 19:32:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 19:32:01       87 阅读
  4. Python语言-面向对象

    2024-04-08 19:32:01       96 阅读

热门阅读

  1. c模板编程c/c++20240401

    2024-04-08 19:32:01       25 阅读
  2. SCP收容物999

    2024-04-08 19:32:01       35 阅读
  3. PTA C 1044 火星文翻译

    2024-04-08 19:32:01       43 阅读
  4. ES-7.12-官网阅读-ILM-index lifecycle actions

    2024-04-08 19:32:01       37 阅读
  5. 设计模式:单例模式

    2024-04-08 19:32:01       39 阅读
  6. 7-32 用天平找小球

    2024-04-08 19:32:01       27 阅读
  7. elasticSearch加入排序sort字段后报错

    2024-04-08 19:32:01       39 阅读
  8. Flutter 关键字

    2024-04-08 19:32:01       33 阅读
  9. ubuntu 22.04安装Anaconda3步骤

    2024-04-08 19:32:01       39 阅读
  10. 谈谈Python中的生成器表达式和它们的优势

    2024-04-08 19:32:01       37 阅读