Concatenate 2 rows with a condition [message #25410] |
Wed, 23 April 2003 07:32 |
Vidya
Messages: 12 Registered: May 2001
|
Junior Member |
|
|
Hi,
I would like to write a query that concatenates the 2 or more rows of a table.
For Eg.
create table a
(no number(2),name varchar2(10))
The data in a is
select * from a
NO NAME
1 vv
1 vi
2 aa
2 bb
I need to write a query that does the following
NO NAME
1 vv,vi
2 aa,bb
Thanks & regards,
Vidya
|
|
|
Re: Concatenate 2 rows with a condition [message #25412 is a reply to message #25410] |
Wed, 23 April 2003 07:53 |
|
Mahesh Rajendran
Messages: 10708 Registered: March 2002 Location: oracleDocoVille
|
Senior Member Account Moderator |
|
|
mag@itloaner1_local > SELECT * FROM EMP;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 13-JUL-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 13-JUL-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
mag@itloaner1_local > CREATE OR REPLACE function fOO (cin in varchar2 ) return
2 varchar2 is
3 retval varchar2(200);
4 cursor c1 is select ENAME from emp WHERE JOB=CIN;
5 begin
6 for crec in c1 loop
7 if c1%rowcount=1 then
8 retval:= crec.ENAME;
9 else
10 retval:=retval||','||crec.ENAME;
11 end if;
12 end loop;
13 return retval;
14 end;
15 /
Function created.
mag@itloaner1_local > SELECT DISTINCT JOB,FOO(JOB) FROM EMP;
JOB FOO(JOB)
--------- ----------------------------------------
ANALYST SCOTT,FORD
CLERK SMITH,ADAMS,JAMES,MILLER
MANAGER JONES,BLAKE,CLARK
PRESIDENT KING
SALESMAN ALLEN,WARD,MARTIN,TURNER
|
|
|
|