To convert columns into row

Hi All,
I need help in building view which actually can show columns data as row.
e.g.
row is as follows
Name Age Salary
ABC 25 10000
BBC 28 12000
The above tables data I want to get as
Name ABC BBC
Age 25 28
Salary 10000 12000
Thanks in advance.

Even if I don't really understand such requirement, I wrote some times ago such function to play around that :
Re: Converting Columns into rows
Nicolas.

Similar Messages

  • Converting columns into rows

    Dear all....I need to convert all columns into rows in a table. For example table has following columns:
    Emp_Cod........Val1......Val2......Val3
    1 a b c
    Now I wish that each column should display as a value like:
    Emp_Cod........Val1
    1 a
    1 b
    1 c
    Now the one way to solve this job is to write a union statement for each column but for this I'll have to write equal number of select statements as there are columns.
    What I need that is there anyway to write minimum code for this job, is there any alternate way???

    SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
      2  select*from t unpivot(v for c in(val1,val2,val3));
    EMP_CODE  C     V                                                      
            1  VAL1  a                                                      
            1  VAL2  b                                                      
            1  VAL3  c                                                      
    SQL> col COLUMN_VALUE for a20
    SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
      2  select*from t,table(sys.odcivarchar2list(val1,val2,val3));
    EMP_CODE  V  V  V  COLUMN_VALUE                                        
            1  a  b  c  a                                                   
            1  a  b  c  b                                                   
            1  a  b  c  c                                                   

  • How to convert columns into rows using  transpose function

    Hi
    anybody tell me how to convert columns values into rows using transpose function.

    Since BluShadow went to all the trouble to put it together, someone should use it.
    See the post titled How do I convert rows to columns? here SQL and PL/SQL FAQ
    John

  • Convert columns into rows

    SQL>create table TEST1
    TRAIN_ID VARCHAR2(10) not null,
    ALIES VARCHAR2(15),
    VAL VARCHAR2(20)
    Now I insert data into test1 table.
    SQL> select* from test2;
    TRAIN_ID NAM DOB
    A ASHIS 11/11/1974
    B SOUGATA 20/06/1977
    Now how can I select data in the following
    output ? Please suggest me.
    TRAIN_ID ALIES VAL
    A NAM ASHIS
    A DOB 11/11/1974
    B NAM SOUGATA
    B DOB 20/06/1977

    SELECT *
      FROM(
    SELECT tran_id, 'NAM' alies, nam val
      FROM test2
    UNION ALL
    SELECT tran_id, 'DOB' alies, TO_CHAR(dob, 'DD/MM/YYYY') val
      FROM test2
    ORDER BY tran_idwould appear to do what you want, though I'm not sure why you'd want it. Note that you'll have to convert all the values to a single data type (in this case VARCHAR2), which would make operating on the data quite painful later on...
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • How to convert  columns into rows

    present result
    Mat_num        comp_code   disc          amount     
    800000     SG01      SAPLF005                    0.00
    800000     SG01      SAPLF005               31,500.00
    6300001    SPM       SAPLF005                    0.00
    6300001    SPM       SAPLF005              108,888.00
    APS100     SMTP      SAPLF005                    0.00
    APS100     SMTP      SAPLF005                    0.00
    required format
    Mat_num       disc             SG01          SMTP          SPM
    800000   SAPLF005   31,500.00           0.00          0.00
    800000   SAPLF005        0.00           0.00          0.00 
    6300001  SAPLF005        0.00         0.00     108,888.00     
    6300001  SAPLF005        0.00         0.00           0.00  
    APS100   SAPLF005        0.00         0.00           0.00 
    APS100   SAPLF005        0.00         0.00           0.00
    Plain Text Attachment [ Scan and Save to Computer ]
    REPORT  Z_CALC2                                 .
    TABLES : KNC1.
    TYPE-POOLS: SLIS.
      type-pools : abap.
    field-symbols: <dyn_table> type standard table,
                   <dyn_wa>,
                   <dyn_field>.
    data: dy_table type ref to data,
          dy_line  type ref to data,
          xfc type lvc_s_fcat,
          ifc type lvc_t_fcat.
    DATA: BEGIN OF ITAB OCCURS 0,
              KUNNR LIKE KNC1-KUNNR,
              BUKRS LIKE KNC1-BUKRS,
              GJAHR LIKE KNC1-GJAHR,
              USNAM LIKE KNC1-USNAM,
              UMSAV LIKE KNC1-UMSAV,
          END OF ITAB.
    *Result table
    TYPES BEGIN OF RES_ITAB.
    * RES_ITAB(10) TYPE C DEFAULE 'ZRES_ITAB'.
    INCLUDE STRUCTURE ZRES_ITAB.
    TYPES END OF RES_ITAB.
    SELECT-OPTIONS : CCODE FOR KNC1-BUKRS DEFAULT 'SG01' TO 'SPM '.
    DATA : BEGIN OF COMP_ITAB OCCURS 0,
              BUKRS LIKE KNC1-BUKRS,
           END OF COMP_ITAB.
    DATA: COLS TYPE I,
          gap(10) TYPE c,
          Company_title type string .
    start-of-selection.
      perform get_NUM_COLS.
      perform get_structure.
      perform create_dynamic_itab.
      perform get_data.
    *  perform write_out.
    FORM GET_NUM_COLS.
    SELECT KUNNR BUKRS GJAHR USNAM UMSAV INTO TABLE ITAB FROM KNC1 WHERE
    BUKRS IN CCODE.
    CLEAR ITAB.
    *CLEAR RES_ITAB.
    LOOP AT ITAB .
         COMP_ITAB-BUKRS = ITAB-BUKRS.
         APPEND COMP_ITAB.
    CLEAR COMP_ITAB.
    ENDLOOP.
    SORT COMP_ITAB BY BUKRS.
    DELETE ADJACENT DUPLICATES FROM COMP_ITAB.
    DESCRIBE TABLE COMP_ITAB LINES COLS.
    ENDFORM.
    *Get table structure
    form get_structure.
    data : idetails type abap_compdescr_tab,
           xdetails type abap_compdescr.
    data : ref_table_des type ref to cl_abap_structdescr.
    data fld_pos type i value 1.
    * Get the structure of the table.
      ref_table_des ?=
                  cl_abap_typedescr=>describe_by_name( 'ZRES_ITAB' ).
      idetails[] = ref_table_des->components[].
      loop at idetails into xdetails.
        clear xfc.
        XFC-COL_POS = fld_pos.
        xfc-fieldname = xdetails-name .
        xfc-datatype = xdetails-type_kind.
        xfc-inttype = xdetails-type_kind.
        xfc-intlen = xdetails-length.
        xfc-decimals = xdetails-decimals.
        append xfc to ifc.
        fld_pos = fld_pos + 1.
      endloop.
      DO  COLS TIMES.
        clear xfc.
        READ TABLE COMP_ITAB INDEX SY-INDEX.
        XFC-COL_POS = fld_pos.
        xfc-fieldname = COMP_ITAB-BUKRS .
        xfc-datatype = 'F'.
    *    xfc-inttype = 'F'.   'details-type_kind.
        xfc-intlen = 14.
    *    xfc-decimals = xdetails-decimals.
        append xfc to ifc.
        fld_pos = fld_pos + 1.
      ENDDO.
      clear xfc.
        XFC-COL_POS = fld_pos.
        xfc-fieldname = 'RECORD_TOTAL' .
        xfc-datatype = 'F'.
        xfc-inttype  = 'F'.
        xfc-intlen = 14.
    *    xfc-decimals = xdetails-decimals.
        append xfc to ifc.
    endform.
    form create_dynamic_itab.
    * Create dynamic internal table and assign to FS
      call method cl_alv_table_create=>create_dynamic_table
                   exporting
                      it_fieldcatalog = ifc
                   importing
                      ep_table        = dy_table.
      assign dy_table->* to <dyn_table>.
    * Create dynamic work area and assign to FS
      create data dy_line like line of <dyn_table>.
      assign dy_line->* to <dyn_wa>.
    endform.
    form get_data.
    LOOP AT ITAB.
            <DYN_WA>-KUNNR = ITAB-KUNNR.
            <DYN_WA>-USNAM = ITAB-USNAM.
            DO COLS TIMES.
               IF <DYN_WA>
    DESCRIBE TABLE <dyn_table>.
    *WRITE:/ SY-TFILL.
    * Select Data from table.
    * <dyn_table> = ITAB[].
    *LOOP AT ITAB.
    *    RES-KUNNR = ITAB-KUNNR.
    *    RES-USNAM = ITAB-USNAM.
    *    CASE ITAB-GJAHR.
    *        WHEN 2005.
    *            RES-UMSAV1 = ITAB-UMSAV.
    *        WHEN 2006.
    *            RES-UMSAV2 = ITAB-UMSAV.
    *        WHEN 2007.
    *            RES-UMSAV3 = ITAB-UMSAV.
    *    ENDCASE.
    *    RES-TOTAL = RES-UMSAV1 + RES-UMSAV2 + RES-UMSAV3 .
    * APPEND RES.
    *ENDLOOP.
    endform.
    form write_out.
      loop at <dyn_table> into <dyn_wa>.
        do.
          assign component  sy-index
             of structure <dyn_wa> to <dyn_field>.
          if sy-subrc <> 0.
            exit.
          endif.
          if sy-index = 1.
            write:/ <dyn_field>.
          else.
            write: <dyn_field>.
          endif.
        enddo.
      endloop.
    endform.
    Edited by: Alvaro Tejada Galindo on Feb 28, 2008 11:56 AM

    solved myself

  • Convert Columns into Rows (internal tables) - Urgent Help Pleasse..

    Hi friends i'm having a little problem and hope you can help me..Here's the situation.
    I have an internal table like shown below
    (Key)                   (Key)               (Key)          (Key)
    PATH_ID     |      GROUP     |      LINE     |     ATRIBUTE          |    VALUE
    ASLN2                1                      1                Company_Code       5146
    ASLN2                1                      1                Account_Code        400405
    ASLN2                1                      1                Profit_Centre          AA00N2
    ASLN2                1                      2                Company_Code       5146
    ASLN2                1                      2                Account_Code       400705
    ASLN2                1                      2                Profit_Centre          AA00N2
    ASLN3                1                      1                Company_Code       5146
    ASLN3                1                      1                Account_Code        400405
    ASLN3                1                      1                Profit_Centre          AA00N2
    ASLN3                1                      2                Company_Code       5146
    ASLN3                1                      2                Account_Code       400705
    ASLN3                1                      2                Profit_Centre          AA00N2
    and i want to convert this internal table to one like below
    PATH_ID      |      GROUP      |      LINE     |      Company Code   |    Account Code    |    Profit Centre
    ASLN2                1                      1                 5146                     400405                   AA00N2
    ASLN2                1                      2                 5146                     400705                   AA00N2
    ASLN3                1                      1                 5146                     400405                   AA00N2
    ASLN3                1                      2                 5146                     400705                   AA00N2
    but i'm a bit of stuck, all those key fields are making me confused...anyone have a marvelous ( ) idea of how to implement this transformation ?
    best regards,
    Ricardo Monteiro

    Itab1 with the structure PATH_ID | GROUP | LINE | ATRIBUTE | VALUE
    Itab2 with structure PATH_ID | GROUP | LINE | ATRIBUTE | VALUE
    Itab3 with the final structure PATH_ID | GROUP | LINE | Company Code | Account Code | Profit Centre
    move itab1 to itab2.
    sort itab2 by path_id group line.
    delete adjacent duplicates from itab2 comparing path_id group line.
    loop at itab2.
      clear itab3.
      move:
         itab2-pathid to itab3-pathid,
         itab2-group to itab3-group,
         itab2-line to itab3-line.  
      loop at itab1where pathid = itab2-pathid
                            and group = itab2-group
                            and line   = itab2-line.
          IF itab1-ATRIBUTE = ' Company_Code'.
             itab3-Company_Code = itab1-attribute.
          elseif itab1-ATRIBUTE = ' Account_Code '.
             itab3-Account_Code = itab1-attribute.
          elseif itab1-ATRIBUTE = ' Profit_Centre ' .
             itab3-profit_center = itab1-attribute.
          endif
      endloop.
      append itab3.
    endloop.
    try this.
    Thanks,
    rajinikanth

  • How to display the rows in to columns and columns into rows?

    DES:- I know by using pivot and unpivot you can convert rows into columns and columns into rows but i don't know how to write the syntax?

    Hi,
    Besides the places Martin mentioned above, there are many examples in the forum FAQ:
    https://community.oracle.com/message/9362005#9362005
    For an example of both PIVOT and UNPIVOT in the same query, see
    http://forums.oracle.com/forums/thread.jspa?threadID=920854&tstart=0

  • How to convert columns to rows

    I have 70 columns and I need to convert them into rows. Please help!
    Currently, it is showing as listed below
    message 1 message 2 message 3 message 4 message 5 .......... message 70
    system 1 20 10 40 60 100
    system 2 40 30 50 80 110
    system 3 60 60 70 90 120
    The desire output
    system 1 system 2 system 3
    message 1 20 40 60
    message 2 10 30 60
    message 3 40 50 70
    message 70

    Something like...
    SQL> ed
    Wrote file afiedt.buf
      1  select decode(rn,1,'Empno :'||empno
      2                  ,2,'Ename ('||empno||') :'||ename
      3                  ,3,'Job ('||empno||') :'||job
      4               ) as col
      5  from emp
      6       cross join (select rownum rn from dual connect by rownum <= 3)
      7* order by empno, rn
    SQL> /
    COL
    Empno :7369
    Ename (7369) :SMITH
    Job (7369) :CLERK
    Empno :7499
    Ename (7499) :ALLEN
    Job (7499) :SALESMAN
    Empno :7521
    Ename (7521) :WARD
    Job (7521) :SALESMAN
    Empno :7566
    Ename (7566) :JONES
    Job (7566) :MANAGER
    Empno :7654
    Ename (7654) :MARTIN
    Job (7654) :SALESMAN
    Empno :7698
    Ename (7698) :BLAKE
    Job (7698) :MANAGER
    Empno :7782
    Ename (7782) :CLARK
    Job (7782) :MANAGER
    Empno :7788
    Ename (7788) :SCOTT
    Job (7788) :ANALYST
    Empno :7839
    Ename (7839) :KING
    Job (7839) :PRESIDENT
    Empno :7844
    Ename (7844) :TURNER
    Job (7844) :SALESMAN
    Empno :7876
    Ename (7876) :ADAMS
    Job (7876) :CLERK
    Empno :7900
    Ename (7900) :JAMES
    Job (7900) :CLERK
    Empno :7902
    Ename (7902) :FORD
    Job (7902) :ANALYST
    Empno :7934
    Ename (7934) :MILLER
    Job (7934) :CLERK
    42 rows selected.

  • Problem in displaying the data of columns into rows in sap script

    hi,
    i am working on a sap script and i have to display the dat which is displayed in column into rows but it is not displaying it properly.
    eg, C
        12.1
        Si
        5.5
    it is displaying the data right now like this but i want to display the  data like this:-
    eg, C      Si
        12.1   5.5
    plzzprovide me guidelines how to solve this problem.

    hi,
    i am using this code to display the data:-
    plzz provide me guidelines where i am getting wrong?
    TOPparCOMPONENT DESP,,,,,, INS. LOT #, , , , , , MIC,,,,,,,,,, MIC VALUEparENDTOPparFINAL
    PROTECT
    IF &I_FINAL-PRUEFLOS& NE '000000000000'
    &I_FINAL-MAKTX(23)&&i_final-prueflos(12Z)&
    &I_FINAL-kurztext(25)&
    &I_FINAL-original_input(8)&
    ELSE
    &I_FINAL-MAKTX(23)&     
    &I_FINAL-kurztext(25)&
    &I_FINAL-original_input(8)&
    ENDIF
    ENDPROTECT
    ITEMHEAD
    POSITION WINDOW
    SIZE WIDTH +0 . 4 CH HEIGHT +1 LN
    BOX FRAME 10 TW
    BOX HEIGHT '1.35' LN INTENSITY 20
    IF &PAGE& = '1'
    BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '43' LN FRAME '10' TW
    For horizontal line at top
    BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
    COLUMN LINES...
    END OF COLUMN LINES...
    BOX XPOS '0' CH YPOS '43' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
    BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '43' LN FRAME '10'TW
    ELSE
    COLUMN LINES...
    END OF COLUMN LINES...
    BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '0' CM WIDTH '45' CM HEIGHT '0' LN FRAME '10' TW
    BOX XPOS '20' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '47' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
    BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '47' LN FRAME '10'TW
    ENDIF
    LINEFEED
    NEWPAGE
    NEW-PAGE
    provide me guidelines to solve this problem.
    Edited by: ricx .s on Mar 13, 2009 5:58 AM

  • Convert columns to row equivalent to stragg function in oracle sql

    Hi,
    Sorry i forgot my Oracle version :
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    CORE    10.2.0.3.0      Production
    TNS for 64-bit Windows: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - ProductionI searched in google but i didn't found the solution.
    I looking for a function in discoverer equivalent to stragg sql function.
    Note : stragg function convert columns to rows.
    Thanks
    SELECT   deptno, stragg ('-' || ename)
        FROM emp_test
    GROUP BY deptno;
        DEPTNO STRAGG_STR                                                 
            10 -CLARK-KING-MILLER                                         
            20 -SMITH-FORD-ADAMS-SCOTT-JONES                              
            30 -ALLEN-BLAKE-MARTIN-TURNER-JAMES-WARD                      
    3 rows selected.Edited by: Salim Chelabi on 2010-01-29 08:32

    Hi again,
    *1- I created  my function in my schema.*
    CREATE OR REPLACE TYPE t_string_agg AS OBJECT
      g_string  VARCHAR2(32767),
      STATIC FUNCTION ODCIAggregateInitialize(sctx  IN OUT  t_string_agg)
        RETURN NUMBER,
      MEMBER FUNCTION ODCIAggregateIterate(self   IN OUT  t_string_agg,
                                           value  IN      VARCHAR2 )
         RETURN NUMBER,
      MEMBER FUNCTION ODCIAggregateTerminate(self         IN   t_string_agg,
                                             returnValue  OUT  VARCHAR2,
                                             flags        IN   NUMBER)
        RETURN NUMBER,
      MEMBER FUNCTION ODCIAggregateMerge(self  IN OUT  t_string_agg,
                                         ctx2  IN      t_string_agg)
        RETURN NUMBER
    SHOW ERRORS
    CREATE OR REPLACE TYPE BODY t_string_agg IS
      STATIC FUNCTION ODCIAggregateInitialize(sctx  IN OUT  t_string_agg)
        RETURN NUMBER IS
      BEGIN
        sctx := t_string_agg(NULL);
        RETURN ODCIConst.Success;
      END;
      MEMBER FUNCTION ODCIAggregateIterate(self   IN OUT  t_string_agg,
                                           value  IN      VARCHAR2 )
        RETURN NUMBER IS
      BEGIN
        SELF.g_string := self.g_string || ',' || value;
        RETURN ODCIConst.Success;
      END;
      MEMBER FUNCTION ODCIAggregateTerminate(self         IN   t_string_agg,
                                             returnValue  OUT  VARCHAR2,
                                             flags        IN   NUMBER)
        RETURN NUMBER IS
      BEGIN
        returnValue := RTRIM(LTRIM(SELF.g_string, ','), ',');
        RETURN ODCIConst.Success;
      END;
      MEMBER FUNCTION ODCIAggregateMerge(self  IN OUT  t_string_agg,
                                         ctx2  IN      t_string_agg)
        RETURN NUMBER IS
      BEGIN
        SELF.g_string := SELF.g_string || ',' || ctx2.g_string;
        RETURN ODCIConst.Success;
      END;
    END;
    SHOW ERRORS
    CREATE OR REPLACE FUNCTION string_agg (p_input VARCHAR2)
    RETURN VARCHAR2
    PARALLEL_ENABLE AGGREGATE USING t_string_agg;
    SHOW ERRORS
    *2- I ran my query in my schema with sqlplus.*
    SELECT deptno,ename,sal, string_agg(ename)over(partition by deptno) AS employees
    FROM   emp_test
    order by deptno;
        DEPTNO ENAME             SAL EMPLOYEES                                        
            10 CLARK            2450 CLARK,KING,MILLER                                
            10 KING             5000 CLARK,KING,MILLER                                
            10 MILLER           1300 CLARK,KING,MILLER                                
            20 JONES            2975 JONES,FORD,ADAMS,SMITH,SCOTT                     
            20 FORD             3000 JONES,FORD,ADAMS,SMITH,SCOTT                     
            20 ADAMS            1100 JONES,FORD,ADAMS,SMITH,SCOTT                     
            20 SMITH             800 JONES,FORD,ADAMS,SMITH,SCOTT                     
            20 SCOTT            3000 JONES,FORD,ADAMS,SMITH,SCOTT                     
            30 WARD             1250 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN             
            30 TURNER           1500 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN             
            30 ALLEN            1600 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN             
            30 JAMES             950 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN             
            30 BLAKE            2850 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN             
            30 MARTIN           1250 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN             
    14 rows selected.
    *3- I import this function in discoverer administration*
    4- My problem :When i use the function string_agg(ename)over(partition by deptno) in discover deskto i got the error you can't use over in this place.
    Any ideas.
    Thank in advance.
    Regards Salim.

  • How to turn columns into rows

    Hi. Does anyone know how to turn columns into rows ie:
    select field1, field2, field3, field4, field5 from table
    desired result:
    field1 field2
    field1 field3
    field1 field4
    field1 field5
    Thank you!

    Something like this ?
    select field1
    , case n.l
    when 1 then field2
    when 2 then field3
    when 3 then field4
    when 4 then field5
    end field
    from table
    , (select level l from dual connect by level <= 4) n

  • Transpose columns into rows

    hi ,
    i need to transpose columns into rows ,
    i know i can use the UNION ALL but my num of columns will most likely not be fixed so how can i do that ?
    pls advise

    This is from one of the forms link,, i reallyy dont know the link, but i guess this is "adrains" code
    SQL> WITH ilv AS (
      2      SELECT str || ','                                   AS str
      3      ,     (LENGTH(str) - LENGTH(REPLACE(str, ','))) + 1 AS no_of_elements
      4      FROM   t
      5      )
      6  SELECT RTRIM(str, ',')                              AS original_string
      7  ,      SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element
      8  ,      element_no
      9  FROM  (
    10         SELECT ilv.str
    11         ,      nt.column_value AS element_no
    12         ,      INSTR(
    13                   ilv.str,
    14                   ',',
    15                   DECODE(nt.column_value, 1, 0, 1),
    16                   DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 1 AS start_pos
    17         ,      INSTR(
    18                   ilv.str,
    19                   ',',
    20                   1,
    21                   DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
    22         FROM   ilv
    23         ,      TABLE(
    24                   CAST(
    25                      MULTISET(
    26                         SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
    27                         ) AS number_ntt )) nt
    28        );
    ORIGINAL_STRING                 SINGLE_ELEMENT  ELEMENT_NO
    X,Y,Z                           X                        1
    X,Y,Z                           Y                        2
    X,Y,Z                           Z                        3
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  XXX                      1
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  Y                        2
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  ZZ                       3
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  AAAAA                    4
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  B                        5
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  CCC                      6
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  D                        7
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  E                        8
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  F                        9
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  GGG                     10
    13 rows selected.
    Note that the above SQL performs the following steps:
        * determines how many elements are in each string (WITH clause);
        * for each string, generates a collection of n elements (TABLE expression), where n is the derived number of elements in the string. Note in particular the use of "less than" in the "CONNECT BY ROWNUM < ilv.no_of_elements" on line 26. In all versions other than 10.1.x, this will need to be "CONNECT BY ROWNUM <= ilv.no_of_elements" (i.e. "less than or equal to"). There is an unusual bug with this row-generation technique in 10.1 that generates an additional row from the CONNECT BY;
        * uses the generated rows in a Cartesian Product with the original data to generate n rows per string, based on the above definition of n;
        * calculates the start and end position of each element in each string (INSTR); and
        * cuts each element from each string (SUBSTR).

  • One Column into Rows

    I have data in a table that looks like below:
    ColumnA               ColumnB
    123                    abc|cde|fgr
    345                    def|ght|sew
    890                    deq|nmk|lop|lip|fre|dwsThere is no limit on how many values you can have in ColumnB and they are pipe delimited.
    I need to split this one column into rows as:
    ColumnA               ColumnB
    123                    abc
    123                    cde
    123                    fgr
    345                    def
    345                    ght
    890                    fre
    890                    dwsThanks in advance!

    with sample_data as (
                         select 123 columna,'abc|cde|fgr' columnb from dual union all
                         select 345,'def|ght|sew' from dual union all
                         select 890,'deq|nmk|lop|lip|fre|dws' from dual
    select  columna,
            regexp_substr(columnb,'[^|]+',1,column_value) columnb
      from  sample_data,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by level <= length(regexp_replace(columnb || '|','[^|]'))
                       as sys.OdciNumberList
      order by columna,
               column_value
       COLUMNA COLUMNB
           123 abc
           123 cde
           123 fgr
           345 def
           345 ght
           345 sew
           890 deq
           890 nmk
           890 lop
           890 lip
           890 fre
       COLUMNA COLUMNB
           890 dws
    12 rows selected.
    SQL> SY.

  • Convert single column into rows

    hi Gurus,
    I have one table test colums are id and name.
    id number
    name varchar2
    data is like
    id name
    1 xy
    2 xyy
    3 mm
    4 pp
    Now my requirement is to convert single column id into rows
    i,e my output should be of singel rows like :- 1,2,3,4
    How to achive this result .
    I dont have any idea to do this query.
    Please help guys.
    Thanks in advance.
    Vijay

    Well,
    As long as your code doesn't have to run in production, simplest way is:
    WM_CONCAT (but it's not documented)
    or use XMLAGG, it's simpler than a connect by:
    MHO%xe> with t as (
      2  select 1 col, 'xy' str from dual union all
      3  select 2, 'xyy' from dual union all
      4  select 3, 'mm'from dual union all
      5  select 4, 'pp' from dual union all
      6  select 8, 'pp' from dual union all
      7  select 12, 'pp' from dual union all
      8  select 40, 'pp' from dual
      9  )-- actual query, based on id's generated above:
    10  select rtrim(xmlagg(xmlelement(e,col||',')).extract('//text()'),',') col
    11  from   t;
    COL
    1,2,3,4,8,12,40
    1 rij is geselecteerd.

  • Need help with query for converting columns to rows

    Hello,
    I know this is a very common question asked in the forum. I have searched regading this, i did find some threads, but i was not able to achieve what i require from the answers posted. So anybody please help me.
    I have a table which is having multiple columns as follows:
    Insert into table_1 (X,Y,Z,A,B,C,D,E,F,G,H,I) values (0,0,2,0,0,1,3,0,0,0,0,0);I want to convert the result into a two column, multiple rows i.e., I want the result as follows:
    Col1 Col2
    X      0
    Y     0
    Z      2
    A     0
    B     0
    C     1
    D     3
    E     0
    F     0
    G     0
    H     0
    I      0Please anybody help me in writing the query for this..

    Is this what you are expecting:
    SQL> WITH T AS
      2  (
      3  SELECT 0 X, 0 Y, 2 Z, 0 A, 0 B, 1 C, 3 D, 0 E, 0 F, 0 G, 0 H, 0 I FROM DUAL
      4  )
      5  SELECT  'X' col1, X col2 FROM T
      6  UNION ALL
      7  SELECT  'Y' col1, Y col2 FROM T
      8  UNION ALL
      9  SELECT  'Z' col1, Z col2 FROM T
    10  UNION ALL
    11  SELECT  'A' col1, A col2 FROM T
    12  UNION ALL
    13  SELECT  'B' col1, B col2 FROM T
    14  UNION ALL
    15  SELECT  'C' col1, C col2 FROM T
    16  UNION ALL
    17  SELECT  'D' col1, D col2 FROM T
    18  UNION ALL
    19  SELECT  'E' col1, E col2 FROM T
    20  UNION ALL
    21  SELECT  'F' col1, F col2 FROM T
    22  UNION ALL
    23  SELECT  'G' col1, G col2 FROM T
    24  UNION ALL
    25  SELECT  'H' col1, H col2 FROM T
    26  UNION ALL
    27  SELECT  'I' col1, I col2 FROM T
    28  /
    C       COL2                                                                   
    X          0                                                                   
    Y          0                                                                   
    Z          2                                                                   
    A          0                                                                   
    B          0                                                                   
    C          1                                                                   
    D          3                                                                   
    E          0                                                                   
    F          0                                                                   
    G          0                                                                   
    H          0                                                                   
    C       COL2                                                                   
    I          0                                                                   
    12 rows selected.

Maybe you are looking for