Thread: how to display empno in a single row instead of displaying it vertically


Permlink Replies: 18 - Pages: 2 [ 1 2 | Next ] - Last Post: Jul 5, 2007 9:04 AM Last Post By: Alessandro Rossi
naveenhks

Posts: 73
Registered: 10/31/00
how to display empno in a single row instead of displaying it vertically
Posted: Nov 10, 2005 11:52 PM
Click to report abuse...   Click to reply to this thread Reply
Hi All,
Greetings!

Requirement :
A select statement to display empno horizontally instead of vertical display at sql prompt.
For Eg:

SQL> Select empno from emp;
empno

9999
2222
3333
4444
5555
...
...
...
...

Instead want the display of data in the below format

empno

9999,2222,3333,4444,5555..........

Thanks in advance.

Regards,
Naveen.
Laurent Schneider

Posts: 5,379
Registered: 02/25/01
Re: how to display empno in a single row instead of displaying it vertically
Posted: Nov 11, 2005 12:16 AM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
SQL> select deptno,
  2    replace( replace( replace(
  3      XMLQUERY('for $cc in ora:view("emp")
let $ename:=$cc/ROW/ENAME/text()
where $cc/ROW/DEPTNO/number()=$deptno/d/number()
return <e>{$ename}</e>'
  4      passing by value xmltype('<d>'||deptno||'</d>') as "deptno"
  5      returning content
  6    ),'</e><e>', ','),'<e>'),'</e>') enames
  7  from dept
    DEPTNO ENAMES
---------- --------------------------------------
        10 CLARK,KING,MILLER
        20 SMITH,JONES,SCOTT,ADAMS,FORD
        30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES
        40
 


http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
naveenhks

Posts: 73
Registered: 10/31/00
Re: how to display empno in a single row instead of displaying it vertically
Posted: Nov 12, 2005 2:31 AM   in response to: Laurent Schneider in response to: Laurent Schneider
Click to report abuse...   Click to reply to this thread Reply
Hi,
Thanks for your response.

I missed out to specify my database version. I am working on below specified version:
Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
PL/SQL Release 8.1.7.4.0 - Production

and moreover without using dept table is it possible to display all the employee names in single line with "," as delimiter.

like a sql query which displays all the employee names present in employee table in a single string with "," as delimiter.

Thanks!

Naveen.
Jens Petersen

Posts: 4,745
Registered: 05/13/99
Re: how to display empno in a single row instead of displaying it vertically
Posted: Nov 12, 2005 2:36 AM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
Pointless

Posts: 4,030
Registered: 05/30/00
Re: how to display empno in a single row instead of displaying it verticall
Posted: Nov 12, 2005 7:53 PM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
SQL> select deptno,
2 max(substr(sys_connect_by_path(ename,','),2)) emps
3 from
4 (
5 select e.deptno, e.ename,
6 row_number() over (
7 partition by e.deptno order by e.ename
8 ) curr,
9 row_number() over (
10 partition by e.deptno order by e.ename
11 ) - 1 prev
12 from emp e, dept d
13 where e.deptno = d.deptno
14 )
15 start with curr = 1
16 connect by prior curr = prev
17 and prior deptno = deptno
18 group by deptno
19 /

DEPTNO EMPS

----------------------------------------
10 CLARK,KING
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
40 MILLER
Jens Petersen

Posts: 4,745
Registered: 05/13/99
Re: how to display empno in a single row instead of displaying it verticall
Posted: Nov 13, 2005 1:32 AM   in response to: Pointless in response to: Pointless
Click to report abuse...   Click to reply to this thread Reply
3360,
sys_connect_by_path does not work on 8i, it was introduced in 9i
naveenhks

Posts: 73
Registered: 10/31/00
Re: how to display empno in a single row instead of displaying it verticall
Posted: Nov 13, 2005 9:23 AM   in response to: Jens Petersen in response to: Jens Petersen
Click to report abuse...   Click to reply to this thread Reply
Can any one help me out in this ............ Plzzzzz
Kamal Kishore

Posts: 7,355
Registered: 09/19/99
Re: how to display empno in a single row instead of displaying it verticall
Posted: Nov 13, 2005 9:48 AM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
Did you read the link posted above by Jens?
Did you try the solution provided there?
naveenhks

Posts: 73
Registered: 10/31/00
Re: how to display empno in a single row instead of displaying it verticall
Posted: Dec 11, 2005 2:05 PM   in response to: Kamal Kishore in response to: Kamal Kishore
Click to report abuse...   Click to reply to this thread Reply
Hi,
I need the output as below :
EMPS

CLARK,KING,ADAMS,FORD,JONES,SCOTT,SMITH,ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD, MILLER

The solution given Jens displays department number wise names , however i want all names to be displayed as mentione above irrespective of department number
Pointless

Posts: 4,030
Registered: 05/30/00
Re: how to display empno in a single row instead of displaying it verticall
Posted: Dec 11, 2005 3:28 PM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
Did you upgrade yet?
naveenhks

Posts: 73
Registered: 10/31/00
Re: how to display empno in a single row instead of displaying it verticall
Posted: Dec 11, 2005 3:34 PM   in response to: Pointless in response to: Pointless
Click to report abuse...   Click to reply to this thread Reply
I am using oracle-8i, is there any possibility to get the result as specified using oracle 8i
Aketi Jyuuzou

Posts: 586
Registered: 03/08/06
Re: how to display empno in a single row instead of displaying it vertically
Posted: Mar 8, 2006 8:06 PM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
create table KeyList(
Key char(3),
Val char(2),
primary key (Key,Val));
insert into KeyList values('AAA','12');
insert into KeyList values('AAA','2A');
insert into KeyList values('AAA','32');
insert into KeyList values('AAA','44');
insert into KeyList values('BBB','11');
insert into KeyList values('BBB','33');
insert into KeyList values('BBB','8S');
commit;
col ConcatVal for a20
select Key,
substr(
replace(
replace(XMLAgg(XMLElement("dummy",Val) order by Val),'</dummy>'),
'<dummy>',',')
,2) as ConcatVal
from KeyList
group by Key;
select Key,max(SubStr(sys_connect_by_path(Val,','),2)) as ConcatVal
from (select Key,Val,
Lag(RowID) over(partition by Key order by Val) as LagRowID
from KeyList)
start with LagRowID is null
connect by Prior RowID = LagRowID
group by Key;

OracleSQLPuzzle
http://oraclesqlpuzzle.hp.infoseek.co.jp
Rangarajan

Posts: 238
Registered: 08/11/05
Re: how to display empno in a single row instead of displaying it verticall
Posted: Mar 8, 2006 9:25 PM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
Dear Naveenhks

select 'empno '||empno||','||chr(15) from emp;

Try this.

Ranga
Jameel

Posts: 1,069
Registered: 03/03/06
Re: how to display empno in a single row instead of displaying it verticall
Posted: Mar 9, 2006 4:29 AM   in response to: Rangarajan in response to: Rangarajan
Click to report abuse...   Click to reply to this thread Reply
hi naveen, try this
SQL> create table tst ( n varchar2(100);
SQL> declare
H varchar2(100):=' ' ;
begin
for R in (select ename from emp) loop
H:=R.ENAME || ' , ' || H ;
end loop;
insert into tst values(H);
end;
/
SQL> SELECT * FROM TST;
MILLER,FORD,JAMES,ADAMS,TURNER,KING,SCOTT,CLARK,BLAKE,MARTIN,JONES,WARD,ALLEN,SMITH

is it what u need ?????
Radhakrishna Sa...

Posts: 2,899
Registered: 01/25/06
Re: how to display empno in a single row instead of displaying it vertically
Posted: Mar 9, 2006 4:38 AM   in response to: naveenhks in response to: naveenhks
Click to report abuse...   Click to reply to this thread Reply
Is this what you are looking for?

SQL> ;
1 select max(substr(sys_connect_by_path(rtrim(ename), ','), 2, 200)) conc
2 from (select ename, rownum rn
3 from emp)
4 start with rn = 1
5* connect by rn = prior rn +1
SQL> /

CONC

KING,BLAKE,CLARK,JONES,MARTIN,ALLEN,TURNER,JAMES,WARD,FORD,SMITH,SCOTT,ADAMS,MILLER

SQL>

Oh! I didn't see if it doesn't work on 8i.

Cheers
Sarma.

Message was edited by:
Radhakrishna Sarma

Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums