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 Go to next message
chandan.rattan
Messages: 84
Registered: December 2008
Location: India
Member

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 Go to previous messageGo to next message
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

icon3.gif  Re: Result from Rows to Columns [message #599803 is a reply to message #599799] Tue, 29 October 2013 02:23 Go to previous messageGo to next message
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 #599804 is a reply to message #599803] Tue, 29 October 2013 02:26 Go to previous messageGo to next message
chandan.rattan
Messages: 84
Registered: December 2008
Location: India
Member

thanks a lot micheal
Re: Result from Rows to Columns [message #599806 is a reply to message #599800] Tue, 29 October 2013 02:27 Go to previous messageGo to next message
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 #599862 is a reply to message #599806] Tue, 29 October 2013 10:26 Go to previous messageGo to next message
mehediu
Messages: 46
Registered: February 2010
Location: Dhaka
Member

if you want to use your own function then may be the following function will help you......

CREATE OR REPLACE FUNCTION rowtocol
    ( p_slct IN VARCHAR2,
     p_dlmtr IN VARCHAR2 DEFAULT ',' ) RETURN VARCHAR2

     AUTHID CURRENT_USER AS
     TYPE c_refcur IS REF CURSOR;
     lc_str VARCHAR2(4000);
     lc_colval VARCHAR2(4000);
     c_dummy c_refcur;
     l number;
     BEGIN
     OPEN c_dummy FOR p_slct;
     LOOP
     FETCH c_dummy INTO lc_colval;
     EXIT WHEN c_dummy%NOTFOUND;
     lc_str := lc_str || p_dlmtr || lc_colval;
     END LOOP;
     CLOSE c_dummy;
     RETURN SUBSTR(lc_str,2);
     EXCEPTION
     WHEN OTHERS THEN
        lc_str := SQLERRM;
        IF c_dummy%ISOPEN THEN
        CLOSE c_dummy;
     END IF;
     RETURN lc_str;
     END;



and use this function ....


select rowtocol('select ename from emp',',')from dual;


Thanks...
Re: Result from Rows to Columns [message #599863 is a reply to message #599862] Tue, 29 October 2013 10:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Learn how to format code using SQL Formatter.

Re: Result from Rows to Columns [message #599867 is a reply to message #599862] Tue, 29 October 2013 10:37 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
mehediu wrote on Tue, 29 October 2013 20:56
if 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 Go to previous messageGo to next message
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 Go to previous message
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.

Previous Topic: Provide the output
Next Topic: Materialized views to rescue "mutating tables"
Goto Forum:
  


Current Time: Fri Apr 19 03:15:26 CDT 2024