|
Replies:
18
-
Pages:
2
[
1
2
| Next
]
-
Last Post:
Jul 5, 2007 9:04 AM
Last Post By: Alessandro Rossi
|
|
|
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
|
|
|
|
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.
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
|
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.
|
|
|
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
|
|
|
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
|
3360,
sys_connect_by_path does not work on 8i, it was introduced in 9i
|
|
|
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
|
|
|
|
Can any one help me out in this ............ Plzzzzz
|
|
|
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
|
|
|
|
Did you read the link posted above by Jens?
Did you try the solution provided there?
|
|
|
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
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
I am using oracle-8i, is there any possibility to get the result as specified using oracle 8i
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
|
Dear Naveenhks
select 'empno '||empno||','||chr(15) from emp;
Try this.
Ranga
|
|
|
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
|
|
|
|
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 ?????
|
|
|
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
|
|
|
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 : 2500
- 1000000
pts
|
|
Expert : 1000
- 2499
pts
|
|
Pro : 500
- 999
pts
|
|
Journeyman : 200
- 499
pts
|
|
Newbie : 0
- 199
pts
|
|
Oracle ACE Director
|
|
Oracle ACE Member
|
|
Oracle Employee ACE
|
|
Helpful Answer
(5 pts)
|
|
Correct Answer
(10 pts)
|
|