Wednesday, July 8, 2015

Using Listagg function

9:25 PM Posted by Dilli Raj Maharjan , 1 comment
 create table dlee(
username varchar2(32),
table_name varchar2(32)
);
/

insert into dlee
values('Anjan','Yamaha');
insert into dlee
values('Anjan','Bike');
insert into dlee
values('Anjan','R15');
insert into dlee
values('Anjan','Gwarko');
insert into dlee
values('Anjan','Dream');
insert into dlee
values('Anjan','CAR');
insert into dlee
values('Dilli','Suzuki');
insert into dlee
values('Dilli','Car');
insert into dlee
values('Dilli','Kirtipur');


Figure 1. Output of table dlee

select
   username,
   listagg (table_name, ',')
WITHIN GROUP
(ORDER BY username) username
FROM
   dlee
GROUP BY
   username
/


Figure 2. Output of listagg output

column table_list format a50
select
   username, ceil(line/3) as Sequence,
   listagg (table_name, ',')
WITHIN GROUP
(ORDER BY username) as table_list
FROM
(select
   username, table_name, rank() over (partition by username order by table_name) as LINE from dlee)
GROUP BY
   username,ceil(line/3)
/


Figure 3. Output of listagg output with 3 list in single line.

1 comment:

  1. do you have dump question for sqlfoundation (1z0-051)exam

    ReplyDelete