我不确定此问题是否在之前的任何地方被问过。我不知道该怎么说呢。但我会用一个场景来解释
我有以下表格
带有列的TAB1:USERID,CODE,COUNTRY
带有列的TAB2:USERID,CODE,EMAIL
示例内容:
TAB1:
RISHI, A1B2C3, INDIA
RISHI, D2E3F4, INDIA
KANTA, G3H4I5, INDONESIA
TAB2:
RISHI, A1B2C3, rishi1@test.com
RISHI, A1B2C3, rishi2@test.com
RISHI, A1B2C3, rishi3@test.com
RISHI, D2E3F4, rishi1@test.com
RISHI, D2E3F4, rishi2@test.com
KANTA, G3H4I5, kanta1@test.com
我想从select查询或pl / sql存储过程得到的结果如下:
RISHI, INDIA, A1B2C3, (rishi1@test.com, rishi2@test.com, rishi3@test.com)
RISHI, INDIA, D2E3F4, (rishi1@test.com, rishi2@test.com)
如果我选择如下:
select a.userid, a.code, a.country, b.email
from tab1.a, tab2.b
where a.userid = b.userid
and a.code = b.code
and a.userid = 'RISHI';
我得到的结果为:
RISHI, INDIA, A1B2C3, rishi1@test.com
RISHI, INDIA, A1B2C3, rishi2@test.com
RISHI, INDIA, A1B2C3, rishi3@test.com
RISHI, INDIA, D2E3F4, rishi1@test.com
RISHI, INDIA, D2E3F4, rishi2@test.com
我基本上需要的是将电子邮件ID组合成一个数组。假设TAB1包含我实际需要的更多列,但在此示例中我省略了,但TAB2只有这三列。
答案 0 :(得分:1)
select a.userid, a.code, a.country, listagg(b.email, ',') within group (order by b.email) as "Emails"
from tab1.a, tab2.b
where a.userid = b.userid
and a.code = b.code
and a.userid = 'RISHI'
group by a.userid, a.code, a.country;
答案 1 :(得分:0)
我想你想在MySQL中使用GROUP_CONCAT
聚合函数。坏消息是Oracle没有用于群组连接的内置功能,好消息是你可以模仿这样的功能。
请看这个片段:
with data
as
(
select job,
ename,
row_number() over (partition by job order by ename) rn,
count(*) over (partition by job) cnt
from emp
)
select job, ltrim(sys_connect_by_path(ename,','),',') scbp
from data
where rn = cnt
start with rn = 1
connect by prior job = job and prior rn = rn-1
order by job
并将返回
JOB SCBP
--------- ----------------------------------------
ANALYST FORD,SCOTT
CLERK ADAMS,JAMES,MILLER,SMITH
MANAGER BLAKE,CLARK,JONES
PRESIDENT KING
SALESMAN ALLEN,MARTIN,TURNER,WARD