Wednesday, November 14, 2007

group_concat functionality in Oracle

some time back I was searching for this query, but couldn't get it. While searching for some
other stuff I got this link
http://www.orafaq.com/forum/m/135323/43055/?srch=concat_all#msg_135323

SQL> select d.dname
2 , NVL(concat_all(concat_expr(e.ename,',')),'None') enames
3 from emp e
4 , dept d
5 where e.deptno(+) = d.deptno
6 group by d.dname
7 /

DNAME ENAMES
-------------- --------------------------------------------------
ACCOUNTING CLARK,KING,MILLER
OPERATIONS None
RESEARCH SMITH,JONES,SCOTT,ADAMS,FORD
SALES ALLEN,MARTIN,TURNER,WARD,JAMES,BLAKE



and this
http://halisway.blogspot.com/2006/08/oracle-groupconcat-updated-again.html

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
/

No comments: