P. 1
Oracle - Row to Column

Oracle - Row to Column

|Views: 364|Likes:
Published by master_coders

More info:

Published by: master_coders on Jun 28, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/30/2014

pdf

text

original

http://www.dba-oracle.com/t_display_multiple_column_values_same_rows.

htm Use a user-defined rowtocol function to convert the rows to columns:
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; / SELECT DISTINCT

a.job, rowtocol( 'SELECT ename FROM emp WHERE job = ' || '''' || a.job || '''' || ' ORDER BY ename') AS Employees FROM emp a; /

3 adult 1239 create or replace as type rc is ref procedure data end.item_id || '.3 575. …. max(decode(item_id. child )) child_' || x. …. end.' || x.3 594.3 839. 10/01/2004 ….4 749. / create or replace as package pivot cursor.3 1199 839. 08/01/2004 ….3 594. …. …. adult_price adult. 12/01/2004 …. end loop. ….http://asktom. …. ….oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:15151874723724 sql> SELECT tr_date. …. …. …. …. …. end. …. for x in ( select distinct item_id from t order by 1 ) loop l_stmt := l_stmt || '.item_id || '. ….item_id. …. ( p_cursor in out rc ).' || x. ….4 719. l_stmt := l_stmt || ' from t group by tr_date order by tr_date'.4 child 874. begin l_stmt := 'select tr_date'. …. …. package body pivot procedure data( p_cursor in out rc ) is l_stmt long. …. …. …. …. …. ….item_id || '. …. adult )) adult_' || x. max(decode(item_id. …. / open p_cursor for l_stmt. …. ITEM_ID 8 9 7661 7659 7661 8 9 588 ADULT 1189 1199 849 759 849 959 1199 1249 CHILD 832.3 531. child_price child FROM mytable WHERE tr_date BETWEEN '01-jan-2004' AND '31-jan-2004' AND tr_code LIKE 'D%' The output is TR_DATE 06/01/2004 06/01/2004 11/01/2004 12/01/2004 12/01/2004 13/01/2004 13/01/2004 13/01/2004 Reqd. . …. ….3 1249 07/01/2004 …. …. item_id. Output: The number of columns (item_id) depend on number of distinct item_id's returned by query Date/Itin_id 8 8 9 9 588 588 589 adult child adult child adult 06/01/2004 1189 832. 13/01/2004 ….

C2.data( :x ).C1(+) AND T1. In your sample data. REPLACE(T1.RN(+) = 4 AND T1.C1 = T3.C2.' ') . you can see that the value "2" in the first column has the largest number of second column entries (a total of 4). c2. can some one please let me know. without resort to a procedural function. If 4 is always the largest number of column2 entries. c2.C1(+) . row_number() over (PARTITION BY c1 ORDER BY c2) rn FROM tab) T4 WHERE T1.com/oracle-development-96/concatenating-multiplerows-of-a-column-into-one-169089.NULL) str FROM (SELECT c1. row_number() over (PARTITION BY c1 ORDER BY c2) rn FROM tab) T1.html I have a table which looks as follows ID Name ----------1 ABC 1 DEF 1 GHI 2 JKL 2 MNO 2 PQR 2 STU 3 VW 3 XYZ I want to write a select statement which gives me the following ouput ID Names ----------------1 ABC-DEF-GHI 2 JKL-MNO-PQR-STU 3 VW-XYZ Is there a way this can be achieved by writing a SELECT statement and not PL/SQL code? If so. Ans-02 Unfortunately. there is an "ugly" fix. concatenate. I appreciate your help Ans-01 The only way I could think of doing that is if you were able to loop through the contents of the table and check while the id's were equal.RN(+) = 3 AND T4. http://forums.C1 = T2. c2. If you desperately need a non-procedural solution.'. row_number() over (PARTITION BY c1 ORDER BY c2) rn FROM tab) T2.C2 || '-' || NVL(T2. The only criteria is that you need to know the MAXIMUM number of rows for the second column for each value in the first column. c2. (SELECT c1.devshed. (SELECT c1.RN(+) = 2 AND T3.C2. row_number() over (PARTITION BY c1 ORDER BY c2) rn FROM tab) T3. there is no way to do this for the general case.RN = 1 AND T2. the following query will work: Code: SELECT T1.variable x refcursor set autoprint on exec pivot.C1(+) AND T1. (SELECT c1.' ') || '-' || NVL(T3.C1.C1 = T4.' ') || '-' || NVL(T4. otherwise go to the next one.'.

where "N" = "the largest number of values for c2 for any given c1". ename.''). of values for ename for any given deptno .rn=1 and e2. row_number() over (PARTITION BY deptno ORDER BY ename) rn FROM emp) (select deptno. '. row_number() over (PARTITION BY deptno ORDER BY ename) rn FROM emp) (select deptno.deptno=e4. This can be expanded by simply adding and joining T5. replace(e1.deptno=e2. . ename. The no.ename.deptno=e3. ' || nvl(e3.' ')|| '. e3.deptno(+) and e1. NULL) Employees FROM (select deptno. '|| nvl(e4.ename. TN. ###################################################### select e1. T6.deptno(+) and e1. ename..deptno.…. e2.deptno(+) e1.rn(+)=4 and e1. e5.rn(+)=2 and e3. row_number() over (PARTITION BY deptno ORDER BY ename) rn FROM emp) (select deptno. . e4 Note: e4.. row_number() over (PARTITION BY deptno ORDER BY ename) rn FROM emp) WHERE e1. '.(where tab is your sample table.' ') ||'.ename||'. '||nvl(e2.ename. and c1 and c2 are the two columns of tab). ename. of select table will depends on (or equal to) = largest No.rn(+)=3 and e4.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->