Home » SQL & PL/SQL » SQL & PL/SQL » Result from Rows to Columns (Oracle 10g,Oracle11g)
Result from Rows to Columns [message #599799] |
Tue, 29 October 2013 01:59 |
|
Hi,
How can i retrieve result of multiple rows into single row. Ex
select ename from emp;
Ename:
--------
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
Result should be like
ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLER
Thanks in advance
|
|
|
Re: Result from Rows to Columns [message #599800 is a reply to message #599799] |
Tue, 29 October 2013 02:07 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
LISTAGG in 11g.
SQL> WITH emp AS(
2 SELECT 'ALLEN' ename FROM dual union ALL
3 SELECT 'WARD' FROM dual union ALL
4 SELECT 'JONES' FROM dual union ALL
5 SELECT 'MARTIN' FROM dual union ALL
6 SELECT 'BLAKE' FROM dual union ALL
7 SELECT 'CLARK' FROM dual union ALL
8 SELECT 'SCOTT' FROM dual union ALL
9 SELECT 'KING' FROM dual union ALL
10 SELECT 'TURNER' FROM dual union ALL
11 SELECT 'ADAMS' FROM dual union ALL
12 SELECT 'JAMES' FROM dual union ALL
13 SELECT 'FORD' FROM dual union ALL
14 SELECT 'MILLER' FROM dual)
15 SELECT LISTAGG(ename, ' , ') WITHIN GROUP (ORDER BY ename) AS employees
16 FROM emp;
EMPLOYEES
--------------------------------------------------------------------------------
ADAMS , ALLEN , BLAKE , CLARK , FORD , JAMES , JONES , KING , MARTIN , MILLER ,
For that matter,
COLLECT function in Oracle 10g
ROW_NUMBER() and SYS_CONNECT_BY_PATH functions in Oracle 9i
Except the undocumented WM_CONCAT(DO NOT USE), other demonstartions are cool here by Tim Hall.
[Updated on: Thu, 06 March 2014 13:28] by Moderator Report message to a moderator
|
|
|
Re: Result from Rows to Columns [message #599803 is a reply to message #599799] |
Tue, 29 October 2013 02:23 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
10g:
SQL> with names as (select ename, row_number() over(order by ename) rn from emp)
2 select substr(sys_connect_by_path(ename,','),2) names
3 from names
4 where connect_by_isleaf = 1
5 connect by prior rn = rn - 1
6 start with rn = 1
7 /
NAMES
-------------------------------------------------------------------------------------
ADAMS,ALLEN,BLAKE,CLARK,FORD,JAMES,JONES,KING,MARTIN,MILLER,SCOTT,SMITH,TURNER,WARD
11g:
SQL> select listagg(ename,',') within group (order by ename) names from emp;
NAMES
-----------------------------------------------------------------------------------
ADAMS,ALLEN,BLAKE,CLARK,FORD,JAMES,JONES,KING,MARTIN,MILLER,SCOTT,SMITH,TURNER,WARD
[Updated on: Tue, 29 October 2013 02:23] Report message to a moderator
|
|
|
|
Re: Result from Rows to Columns [message #599806 is a reply to message #599800] |
Tue, 29 October 2013 02:27 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Another way, 9i and up, xmlagg :
SQL> WITH emp AS(
2 SELECT 'ALLEN' ename FROM dual union ALL
3 SELECT 'WARD' FROM dual union ALL
4 SELECT 'JONES' FROM dual union ALL
5 SELECT 'MARTIN' FROM dual union ALL
6 SELECT 'BLAKE' FROM dual union ALL
7 SELECT 'CLARK' FROM dual union ALL
8 SELECT 'SCOTT' FROM dual union ALL
9 SELECT 'KING' FROM dual union ALL
10 SELECT 'TURNER' FROM dual union ALL
11 SELECT 'ADAMS' FROM dual union ALL
12 SELECT 'JAMES' FROM dual union ALL
13 SELECT 'FORD' FROM dual union ALL
14 SELECT 'MILLER' FROM dual)
15 SELECT RTRIM(XMLAGG(XMLELEMENT(E, ENAME || ',')).EXTRACT('//text()'), ',') ENAMES
16 FROM EMP;
ENAMES
--------------------------------------------------------------------------------
ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLER
|
|
|
|
|
Re: Result from Rows to Columns [message #599867 is a reply to message #599862] |
Tue, 29 October 2013 10:37 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mehediu wrote on Tue, 29 October 2013 20:56if you want to use your own function then may be the following function will help you......
CREATE OR REPLACE FUNCTION rowtocol
.........
EXCEPTION
WHEN OTHERS THEN
lc_str := SQLERRM;
IF c_dummy%ISOPEN THEN
CLOSE c_dummy;
END IF;
RETURN lc_str;
END;
Ok, you are using WHEN OTHERS to close any open cursor. But without a RAISE?
|
|
|
Re: Result from Rows to Columns [message #607429 is a reply to message #599803] |
Thu, 06 February 2014 06:14 |
jimit_shaili
Messages: 237 Registered: June 2006 Location: India, Ahmedabad
|
Senior Member |
|
|
Dear Michel
In oracle 11g, if two of the employees have same name then how to get distinct employee names through listagg function. Infect i want to know is there any other way to get unique employee names by using listagg function.
Regards
Jimit
[Updated on: Thu, 06 February 2014 06:16] Report message to a moderator
|
|
|
Re: Result from Rows to Columns [message #607432 is a reply to message #607429] |
Thu, 06 February 2014 07:25 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
You have to first get unique names then aggregate.
The actual query depends on your actual case.
You can also write your own aggregate function like T. Kytes's stragg.
|
|
|
Goto Forum:
Current Time: Fri Apr 19 03:15:26 CDT 2024
|