1、两个字段表,id和name,其中id和name一对多关系,需要列转行操作:

create table test(id number, name varchar2(20));

insert into test values(1, 'a');

insert into test values(2, 'b');--添加多条数据,一对多

select tmp.id,
  max(decode(tmp.row_number, 1, name)) as c1,
  max(decode(tmp.row_number, 2, name)) as c2,
  max(decode(tmp.row_number, 3, name)) as c3,
  max(decode(tmp.row_number, 4, name)) as c4,
  max(decode(tmp.row_number, 5, name)) as c5
from (select t.*, row_number() over(partition by t.id order by name) as row_number from test t) tmp
group by tmp.id

缺点:1对n,需要添加n个max行,少量的统计可以这样做。

 

2、行转列,将n个字段通过字符连接成一个字段进行展示:

oracle 12g通过listagg函数实现,可自行百度。

posted on 2020-07-24 16:37  dysdhd  阅读(82)  评论(0编辑  收藏  举报