I just came across this interesting article here , showing how to mimic wm_concat() or group_concat() in Oracle using hierarchical query and window functions:
SELECT deptno, LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees FROM (SELECT deptno, ename, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev FROM emp) GROUP BY deptno CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno START WITH curr = 1;
Although, I think this is not a very readable solution, it is quite interesting, especially because the CONNECT BY .. STARTS WITH appears after the GROUP BY . According to the specification , this should not be possible. I tried this using a simple query, but it works! The following two queries return the same results:
-- wrong according to the specification: select level from dual group by level connect by level <= 2; -- correct according to the specification: select level from dual connect by level <= 2 group by level;
Is this an undocumented function? Or just syntactic indifference for convenience? Or are these two statements subtly behaving differently?
Lukas Eder
source share