17,377
社区成员
发帖
与我相关
我的任务
分享
select a.dhhm,a.bz,wm_concat(b.un) un
from
(select dhhm,a1.xm1||'|'||a1.xxk un from a1
union all
select dhhm,xm2||'|'||xxk un from a2) b,a
where a.dhhm=b.dhhm
group by a.dhhm,a.bz
Connected to Oracle Database 11g Enterprise Edition Release 11.1.0.7.0
Connected as topeng
SQL>
SQL> with a as
2 (
3 select '13311111111'DHHM, 'test01' BZ from dual
4 union all
5 select '13322222222', 'test02' from dual
6 union all
7 select '13333333333', 'test03' from dual
8 ),
9 a1 as
10 (
11 select '13311111111' DHHM1, 'wangjj' XM1, '电话库' XXK from dual
12 union all
13 select '13322222222' DHHM1, 'chenyy' XM1, '电话库' XXK from dual
14 union all
15 select '13322222222' DHHM1, 'gogogo' XM1, '电话库' XXK from dual
16 union all
17 select '13333333333' DHHM1, 'kkkkk' XM1, '电话库' XXK from dual
18 )
19 ,a2 as
20 (
21 select '13333333333' DHHM2, 'aaaaaa' XM2,'人口库' XXK from dual
22 union all
23 select '13322222222' DHHM2, 'bbbbbb' XM2,'人口库' XXK from dual
24 )
25 select t.dhhm,wm_concat(temp),t.bz from
26 (
27 select a.DHHM, a2.XM2||'|'||a2.XXK temp,a.BZ from a,a2 where a.DHHM=a2.DHHM2
28 union all
29 select a.DHHM, a1.XM1||'|'||a1.XXK temp,a.BZ from a,a1 where a.DHHM = a1.DHHM1
30 ) t group by t.dhhm,t.BZ
31 /
DHHM WM_CONCAT(TEMP) BZ
----------- -------------------------------------------------------------------------------- ------
13311111111 wangjj|电话库 test01
13322222222 bbbbbb|人口库,chenyy|电话库,gogogo|电话库 test02
13333333333 aaaaaa|人口库,kkkkk|电话库 test03
SQL>
with a as
(
select '13311111111'DHHM, 'test01' BZ from dual
union all
select '13322222222', 'test02' from dual
union all
select '13333333333', 'test03' from dual
),
a1 as
(
select '13311111111' DHHM1, 'wangjj' XM1, '电话库' XXK from dual
union all
select '13322222222' DHHM1, 'chenyy' XM1, '电话库' XXK from dual
union all
select '13322222222' DHHM1, 'gogogo' XM1, '电话库' XXK from dual
union all
select '13333333333' DHHM1, 'kkkkk' XM1, '电话库' XXK from dual
)
,a2 as
(
select '13333333333' DHHM2, 'aaaaaa' XM2,'人口库' XXK from dual
union all
select '13322222222' DHHM2, 'bbbbbb' XM2,'人口库' XXK from dual
)
select t.dhhm,wm_concat(temp),t.bz from
(
select a.DHHM, a2.XM2||'|'||a2.XXK temp,a.BZ from a,a2 where a.DHHM=a2.DHHM2
union all
select a.DHHM, a1.XM1||'|'||a1.XXK temp,a.BZ from a,a1 where a.DHHM = a1.DHHM1
) t group by t.dhhm,t.BZ
Connected to Oracle Database 11g Enterprise Edition Release 11.1.0.7.0
Connected as topeng
SQL>
SQL> with a as
2 (
3 select '13311111111'DHHM, 'test01' BZ from dual
4 union all
5 select '13322222222', 'test02' from dual
6 union all
7 select '13333333333', 'test03' from dual
8 ),
9 a1 as
10 (
11 select '13311111111' DHHM1, 'wangjj' XM1, '电话库' XXK from dual
12 union all
13 select '13322222222' DHHM1, 'chenyy' XM1, '电话库' XXK from dual
14 union all
15 select '13322222222' DHHM1, 'gogogo' XM1, '电话库' XXK from dual
16 union all
17 select '13333333333' DHHM1, 'kkkkk' XM1, '电话库' XXK from dual
18 )
19 ,a2 as
20 (
21 select '13333333333' DHHM2, 'aaaaaa' XM2,'人口库' XXK from dual
22 union all
23 select '13322222222' DHHM2, 'bbbbbb' XM2,'人口库' XXK from dual
24 )
25 select t.dhhm,wm_concat(temp) from
26 (
27 select a.DHHM, a2.XM2||'|'||a2.XXK temp from a,a2 where a.DHHM=a2.DHHM2
28 union all
29 select a.DHHM, a1.XM1||'|'||a1.XXK temp from a,a1 where a.DHHM = a1.DHHM1
30 ) t group by t.dhhm
31 /
DHHM WM_CONCAT(TEMP)
----------- --------------------------------------------------------------------------------
13311111111 wangjj|电话库
13322222222 bbbbbb|人口库,chenyy|电话库,gogogo|电话库
13333333333 aaaaaa|人口库,kkkkk|电话库
SQL>
--1
select DHHM,XM1||'|'||XXK col2,BZ
from a,a1
where a.DHHM=a1.DHHM1
union all
select DHHM,XM2||'|'||XXK col2,BZ
from a,a2
where a.DHHM=a2.DHHM2
--2.先自己写过函数然后再语句中调用
--创建个视图
create or replace view test
as
select DHHM,XM1||'|'||XXK col2,BZ
from a,a1
where a.DHHM=a1.DHHM1
union all
select DHHM,XM2||'|'||XXK col2,BZ
from a,a2
where a.DHHM=a2.DHHM2
create or replace function my_concat(var_col varchar2)
return varchar2
is
v_col varchar2(4000);
begin
for cur in (select col2 from test where dhhm=var_col)
loop
v_col:=v_col||cur.col2||';';
end loop;
v_col:=rtrim(v_col,',');
return v_col;
end;
--最后从视图中读取数据
select DHHM,my_concat(col2),BZ
FROM TEST;
select a.dhhm,b.xx,a.bz
from a,
(select DHHM1,wm_concat(xm1||'|'||xxk) xx from (
select DHHM1, XM1, XXK from a1
union all
select DHHM2 DHHM1, XM2 XM1, XXK from a2)
group by DHHM1) b
where a.dhhm=b.dhhm