I have table like below
ID Name
1 a
1 b
1 c
2 d
2 e
3 f
I would like to get result as
ID Name
1 a,b,c
2 d,e
3 f
I don't want to use any XMLPATH
or coalesce
functions.
Just in simple SQL query I need to get the expected result.
I have table like below
ID Name
1 a
1 b
1 c
2 d
2 e
3 f
I would like to get result as
ID Name
1 a,b,c
2 d,e
3 f
I don't want to use any XMLPATH
or coalesce
functions.
Just in simple SQL query I need to get the expected result.
Since you are on Oracle 10g
version, you cannot use LISTAGG
. It was introduced in 11g
.
And please DON'T use WM_CONCAT as it is an undocumented feature, and has been removed from the latest release. See Why does the wm_concat not work here?
For 10g, you have following string aggregation techniques:
Following is a pure SQL method using ROW_NUMBER()
and SYS_CONNECT_BY_PATH
functions available since 9i:
SQL> column emp_list format a50
SQL> SELECT deptno,
2 LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,','))
3 KEEP (DENSE_RANK LAST ORDER BY cur),',') AS emp_list
4 FROM (SELECT deptno,
5 ename,
6 ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS cur,
7 ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
8 FROM emp)
9 GROUP BY deptno
10 CONNECT BY prev = PRIOR cur AND deptno = PRIOR deptno
11 START WITH cur = 1;
DEPTNO EMP_LIST
---------- --------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
SQL>
In Oracle 10 you can use the inofficial function WM_CONCAT for this. In later versions you'd use LISTAGG.
select id, wm_concat(name)
from mytable
group by id;