sql - Oracle用逗号分隔值选择四个表的值
问题描述
我有四个表,名称分别是APP_PROFILE
、ORIG
和。我想选择它们的值并用逗号分隔它们。顺便说一句,我找到了函数,但据我所知,函数不能工作多列。TERM
TERM_FAIL
listagg
listagg
我的表格如下;
Table: APP_PROFILE
-----------------------
ID NAME
-- ------------
1 profile_anil
2 profile_anil2
Table: ORIG
-----------------------
PROFILE_NAME ORIG_ID
------------ ---------
profile_anil 3
profile_anil 4
profile_anil2 5
profile_anil2 6
Table: TERM
-----------------------
PROFILE_NAME TERM_ID
------------ ---------
profile_anil 7
profile_anil 8
profile_anil2 9
profile_anil2 10
Table: TERM_FAIL
-----------------------
PROFILE_NAME TERM_FAIL_ID
------------ -------------
profile_anil 11
profile_anil 12
profile_anil2 13
profile_anil2 14
Table: Result
-----------------------
PROFILE_NAME ORIG_ID TERM_ID TERM_FAIL_ID
------------ --------- --------- -------------
profile_anil 3,4 7,8 11,12
profile_anil2 5,6 9,10 13,14
解决方案
您可以使用子查询:
select ap.*,
(select listagg(orig_id, ',') within group (order by orig_id)
from orig o
where o.profile_name = ap.profile_name
) as origs,
(select listagg(term_id, ',') within group (order by term_id)
from term t
where t.profile_name = ap.profile_name
) as terms,
(select listagg(term_fail_id, ',') within group (order by term_fail_id)
from term_fail tf
where tf.profile_name = ap.profile_name
) as term_fails
from app_profile ap;
推荐阅读
- python - Pandas Dataframe Python:代码错误修复和简化 - 如果 Location='Y',商店类别级别的空间减少 50%
- sql - Oracle HR 模式中的 SQL
- groovy - groovy中最小调用中的比较器与闭包?
- amazon-web-services - 从 AWS Lambda 函数中删除 zip 文件
- python - 更新的 pandas 显示旧版本并在使用爆炸功能时引发属性错误
- python - 按日期加密密码实验
- javascript - axios 无法获取响应数据
- image-processing - 想不出解决这个图像分割问题
- c - 如何在具有 Web 扩展原生消息传递 API 的原生 C 应用程序中使用`stdin`?
- swift - 如何删除上一个单元格的 UITableViewCell 分隔符?