资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

oracle如何一行多行,oracle多行函数

Oracle一列的多行数据拼成一行显示

Oracle 提供了两个函数wmsys.wm_concat 和 LISTAGG函数。

创新互联公司专注于裕华网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供裕华营销型网站建设,裕华网站制作、裕华网页设计、裕华网站官网定制、小程序开发服务,打造裕华网络公司原创品牌,更为您提供裕华网站排名全网营销落地服务。

wmsys.wm_concat是Oracle 10g推出的,用来连接字符串,wmsys.wm_concat()中的参数也可以使多个,使用”||”拼接,如下例子:select deptno,wmsys.wm_concat(ename || '-' || job) name from emp group by deptno;

LISTAGG是Oracle 11g推出的,它的作用和wmsys.wm_concat是一样。基础语法:LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)。用法就像聚合函数一样,通过Group by语句,把每个Group的一个字段,拼接起来。非常方便。

同样是聚合函数,还有一个高级用法:就是over(partition by XXX),也就是说,在你不使用Group by语句时候,也可以使用LISTAGG函数:

使用实例:

结果:

oracle一行转多行

遇到这样的一个需求,需要实现根据一个字段统计数据,但是这个字段存储是多个字根据逗号分隔存储的,我要实现一行转多行的逻辑。

转化后:

oracle如何1行转多行

对你的问题我写了示例:

Connected to Oracle9i Enterprise Edition Release 9.2.0.4.0

Connected as test

SQL

SQL drop table taba;

Table dropped

SQL create table taba(c1 number,c2 number,c3 number);

Table created

SQL insert into taba values(11,12,13);

1 row inserted

SQL insert into taba values(14,15,16);

1 row inserted

SQL commit;

Commit complete

SQL select * from taba a,(select rownum rn from dual connect by rownum = 3) b order by a.c1,b.rn;

C1 C2 C3 RN

---------- ---------- ---------- ----------

11 12 13 1

11 12 13 2

11 12 13 3

14 15 16 1

14 15 16 2

14 15 16 3

6 rows selected

SQL select decode(b.rn,1,a.c1,2,a.c2,3,a.c3,0) col from taba a,(select rownum rn from dual connect by rownum = 3) b

2 order by decode(b.rn,1,a.c1,2,a.c2,3,a.c3,0);

COL

----------

11

12

13

14

15

16

6 rows selected

----------------------------------

SQL

Connected to Oracle9i Enterprise Edition Release 9.2.0.4.0

Connected as test

SQL

SQL drop table taba;

Table dropped

SQL drop table tabb;

Table dropped

SQL create table taba(c1 number,c2 number,c3 number);

Table created

SQL insert into taba values(11,12,13);

1 row inserted

SQL insert into taba values(14,15,16);

1 row inserted

SQL commit;

Commit complete

SQL create table tabb(c1 number);

Table created

SQL /*此表可用临时表,数据行数取决于希望变成的行数*/

2 insert into tabb values(1);

1 row inserted

SQL insert into tabb values(2);

1 row inserted

SQL insert into tabb values(3);

1 row inserted

SQL commit;

Commit complete

SQL select * from taba a,tabb b order by a.c1,b.c1;

C1 C2 C3 C1

---------- ---------- ---------- ----------

11 12 13 1

11 12 13 2

11 12 13 3

14 15 16 1

14 15 16 2

14 15 16 3

6 rows selected

SQL select decode(b.c1,1,a.c1,2,a.c2,3,a.c3,0) col from taba a,tabb b

2 order by decode(b.c1,1,a.c1,2,a.c2,3,a.c3,0);

COL

----------

11

12

13

14

15

16

6 rows selected

oracle一行变为多行显示,

取第三个空格

然后截取

select * from

(

select substr(col_name,0,instr(col_name,' ',0,3)),rowid from ....

union all

select substr(col_name,instr(col_name,' ',0,3),lenth(col_name)-instr(col_name,' ',0,3)),rowid from .....

) t order by t.rowid

具体中间有些差异,你适当调整一下截取的范围

oracle 一行 对应 多行 合并 新增字段

应该不是很难,用case when进行列转行,然后group by合并,然后利用userid和b表关联。

关联那块不用写了吧,case when转换那块大概的意思是:

select userid,max(case when indexname='年龄' then indexvalue end) 年龄,(后面用类似的方法写性别,血型,地址,电话) from A group by userid

这样查询出来的结果应该是userid,年龄,性别,血型,地址,电话,然后再用这张表和b表关联就行了。直接写应该也可以(没有环境试验)不过应该要麻烦一些,你可以试试。


名称栏目:oracle如何一行多行,oracle多行函数
标题网址:http://cdkjz.cn/article/hodpge.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220