Transpose columns into rows-Please Help

Hi,
I have a table(test_data) in following format:
F_P            reference_id  first_nm  last_nm
EM_NULL                   0         0        0
EM_NOT_NULL               0         0        0
DM_NULL                    1296       145     1689
DM_NOT_NULL               0         0        0I need to transpose the the above table as :
Column_Name   EM_NULL EM_NOT_null DM_null DM_not_null
Reference_ID        0             0    1296           0
first_nm            0             0     145           0
last_nm                 0             0    1689           0I tried below code but unable to get the first column values as reference_id,first_nm,last_nm although I was successful in getting the other column values.Below is the code:
With
  lines as (select level line from dual connect by level <= 3),
    cols as (select level col from dual connect  by level <= 3)
    select
    max(decode(col, 1,
                      decode(line, 1, to_number('REFERENCE_ID') ,
                                   2, to_number('FIRST_NM'),
                                   3, to_number('LAST_NM')
                                   )))column_name,
    max(decode(col, 2,
                      decode(line, 1, REFERENCE_ID,
                                   2, FIRST_NM,
                                   3, LAST_NM
                                   )))EM_NULL,
         max(decode(col, 3,
                     decode(line, 1, REFERENCE_ID,
                                   2, FIRST_NM,
                                   3, LAST_NM
                                   )))EM_NOT_NULL,
          max(decode(col, 4,
                     decode(line, 1, REFERENCE_ID,
                                  2, FIRST_NM,
                                   3, LAST_NM
                                   )))DM_NULL,
          max(decode(col, 5,
                     decode(line, 1, REFERENCE_ID,
                                   2, FIRST_NM,
                                   3, LAST_NM
                                   )))DM_NULL
   from lines, cols,
        (select rownum rn, TEST_DATA.* from Test_Data)
   where rn = col
   group by line
   order by lineBut since my 1st column values are character its not allowing me the group by function.
Edited by: BluShadow on 23-Sep-2011 08:31
formatted and added {noformat}{noformat} tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

I think you want something like this...
SQL> ed
Wrote file afiedt.buf
  1  with test_data as (select 'EM_NULL' as f_p, 0 as reference_id, 0 as first_nm, 0 as last_nm from dual union all
  2                     select 'EM_NOT_NULL', 0, 0, 0 from dual union all
  3                     select 'DM_NULL', 1296, 145, 1689 from dual union all
  4                     select 'DM_NOT_NULL', 0, 0, 0 from dual)
  5  --
  6  -- end of test data
  7  --
  8  select decode(rn,1,'Reference_ID',2,'first_nm',3,'last_nm') as Column_name
  9        ,max(decode(f_p,'EM_NULL',decode(rn,1,reference_id,2,first_nm,3,last_nm))) as EM_NULL
10        ,max(decode(f_p,'EM_NOT_NULL',decode(rn,1,reference_id,2,first_nm,3,last_nm))) as EM_NOT_NULL
11        ,max(decode(f_p,'DM_NULL',decode(rn,1,reference_id,2,first_nm,3,last_nm))) as DM_NULL
12        ,max(decode(f_p,'DM_NOT_NULL',decode(rn,1,reference_id,2,first_nm,3,last_nm))) as DM_NOT_NULL
13  from test_data, (select rownum rn from dual connect by rownum <= 3)
14  group by decode(rn,1,'Reference_ID',2,'first_nm',3,'last_nm')
15* order by decode(Column_name,'Reference_ID',1,'first_nm',2,3)
SQL> /
COLUMN_NAME     EM_NULL EM_NOT_NULL    DM_NULL DM_NOT_NULL
Reference_ID          0           0       1296           0
first_nm              0           0        145           0
last_nm               0           0       1689           0Unless you're using 11g and then you could look at the new PIVOT/UNPIVOT keywords

Similar Messages

  • 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).

  • Report crashes when table having lots of columns and rows, please help

    Hi, Everyone,
    Our system is Apex 4.0.2 with Oracle 11GR2 and Oracle HTTP server in the middle.
    We have a very simple report drawing data from a table with 46 columns ( no binary data) , when the maximun rows is 500, everything is fine; but if I increase the maximum rows to 500000, it crashes repeatedly. We are doing third party database integration , our goal is using Apex simple reports with CSV exporting capability to export tables into text files which can be imported by other databases. I know there are other ways to export table into CSV file, but APEX report seems very easy to use, best of all, non programmers can use it too....
    Our experience shows that: if the table has few columns, like 10 columns, we can use report to dump large number of rows like 200000, but we can't use APEX report for table with lots of columns. the HTTP server log doesn't show any meaningful information. ( we use the Apex Utilities like UNLOAD data with the same crash result).
    Any suggestions are greatly appreciated.

    "Disk cannot be read from or written to" error syncing iPod in iTunes.

  • 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.

  • Transpose columns and rows in numbers

    I need to transpose columns and rows in Numbers and I do not want to write script to do it.  Is there an easier way?

    Give me a proper transpose and I will uninstall Excel and never look back.
    Ok, here's a proper transpose, that can be placed in an Automator Service so it becomes a simple menu pick as below (and can also be assigned a keyboard shortcut).
    To use it (this is slightly different from Excel) you select the range you want to transpose, choose Copy Transpose, click a destination cell in an existing table in the current document or another document, and command-v (Edit > Paste) or option-shift-command-v (Edit > Paste and Match Style).
    The one-time setup is as follows.  In Automator choose File > New > Service,  drag a Run AppleScript action from the left into the right pane, choose 'No Input' for 'Services receives selected' and 'Numbers' for 'in'. Then paste the following into the Run AppleScript action, replacing all text already there by default:
    --Transpose - select range, run, paste transposed values where wanted
    try
              tell application "Numbers" to tell front document to tell active sheet
                        set selected_table to first table whose class of selection range is range
                        tell selected_table
                                  set my_selection to the selection range
                                  set first_col to address of first column of my_selection
                                  set last_col to address of last column of my_selection
                                  set first_row to address of first row of my_selection
                                  set last_row to address of last row of my_selection
                                  set str to ""
                                  repeat with i from first_col to last_col
                                            repeat with j from first_row to last_row
                                                      set str to str & (value of cell j of column i of selected_table) & tab
                                            end repeat
                                            set str to str & return -- add line return after row
                                  end repeat
                        end tell
              end tell
      set the clipboard to str
              display notification "Ready to paste transposed values" with title "Numbers"
    on error
              display dialog "Select a range first and then try again"
    end try
    --end script
    Hit the compile "hammer" and the script should indent properly. Then save the service with the name you want to appear in your menu, and it will thereafter be available via the Services menu (and keyboard shortcut, if you set one up in System Preferences > Keyboard > Shortcuts > Services).
    That's it. Less then five minutes' one-time set-up work and you've got a menu pick for a transpose functionality that is as convenient as Excel's.
    SG

  • How to transpose columns to rows in ODI

    Hi,
    I am trying to transpose Columns to rows along with Headers. For instance if i have
    Account Name | Assigned To | Lead Source | Currency | Onsite_April |Offisite_April2 | FCST AMC LIC April
    | | | | | |
    Microsoft | Mark | Channel1 | INR | 1000 | 2000 | 3000
    Then i need in the below way
    Account Name | Assigned To | Lead Source | Currency | Month | Amount
    | | | | |
    Microsoft | Mark | Channel1 | INR | Onsite_April | 1000
    Microsoft | Mark | Channel1 | INR | Offisite_April2 | 2000
    Microsoft | Mark | Channel1 | INR | FCST AMC LIC April | 3000
    Is there any specific function where i can achieve this?
    More over i need break up Month column into two i.e Onsite_April will be Onsite (in one column) April (in another column).

    Hi
    here is what i got by using the above KM
    create table cad_temp_1(item_id number, location_id number, sales_date_ud1 varchar2(100), qty_ud2 number);
    create table cad_temp_2(item_id number, location_id number, D01012010 number, D01022010 number);
    insert into cad_temp_2 values(1,1,200,300);
    commit;
    Source in interface is : cad_temp_2
    Target is: cad_temp_1
    Mapping is
    Item_id - Item_id execute on: CAD_TEMP_2 (Source selected) Update - Insert and Update checked
    Location_id - Location_id execute on: CAD_TEMP_2 (Source selected) Update - Insert and Update checked
    sales_date_ud1 - :NA execute on: <null> (Target selected) Update - Insert, Update, UD1 selected
    qty_ud2 - :NA execute on : <null> (Target selected) Update - Insert, Update, UD2 selected.
    Execute the interface
    It works.
    Thanks
    Bhaskar

  • Transposing columns to rows in excel through ODI

    Hi all,
    We are trying to transpose Columns to rows along with Headers considering excel as source and oracle table as target
    For example we are having metadata and data in Excel sheet like below
    Metadata:- A,B,TC1,TD1,L1,U1,TC2,TD2,L2,U2,TC3,TD3,L3,U3
    Data:-        X,Y,1,2,3,4,5,6,7,8,9,10,11,12
                     M,N,a,b,c,d,e,f,g,h,i,j,k,l
    Then we need in the below way in oracle table
    A,B,TC1,TD1,L1,U1
    X,Y,1,2,3,4
    X,Y,5,6,7,8
    X,Y,9,10,11,12
    M,N,a,b,c,d
    M,N,e,f,g,h
    M,N,i,j,k,l
    Is there any process to achieve this?
    If so Please let us know
    Thanks in advance

    Hi SH,
    Thanks for your reply
    We already tried http://s3.amazonaws.com/Ora/KM_IKM_Pivot.zip by following the link https://community.oracle.com/thread/904535?tstart=0
    But unfortunately we got the same error which is mentioned in that link
    org.apache.bsf.BSFException: exception from Jython:
    Traceback (innermost last):
    File "<string>", line 35, in ?
    java.sql.SQLException: Invalid column name
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
    I am newbie to odi - if possible can you please guide me how to do transpose in odi by considering excel as source and oracle as target
    Thanks

  • Transpose columns to rows

    HI All,
    I want to transpose columns to rows in my reports. I tried through Pivot Table and i was able to do that, but columns name are not appearing in the left.
    Plz help me out how to do that with column name/
    Can we do the same with measure also???
    Thanks

    All, thanks for the information.
    I have a similar requirement as being discussed here. However, I do not have any measures in my report. It is a plain request which fetches data in three columns A, B and C (and variable number of rows depending on the data available).
    Can I still use pivot table to transpose this data such that the resultant display would have three rows always and variable number of columns, with the first column containing the header A, B and C ? If yes, how do I go about it? Else, is there any other approach?
    Thanks again
    I
    Edited by: user635102 on Sep 18, 2008 12:31 PM

  • 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

  • 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 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

  • 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                                                   

  • 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.

  • I am having a problem converting word onto PDF . PDF into word.Please help .I am signed and paid to Feb 2015

    Can someone advise me why I am having this problem

    Always in the past .I have my Word Document on screen and then I go to 
    Publish and it atomatically PDF the doc.
    The same if I have a PDF to convert to word. I have the PDF on screen and 
    to the right of my screen it will say Convert .I press on convert it
    converts to  word
    In a message dated 12/17/2014 10:01:32 A.M. Eastern Standard Time, 
    [email protected] writes:
    I  am having a problem converting word onto PDF . PDF into word.Please help
    .I am signed and paid to Feb 2015
    created by florencejohn (https://forums.adobe.com/people/florencejohn) 
    in  Adobe Acrobat.com Services - View the full  discussion
    (https://forums.adobe.com/message/7023171#7023171)

  • 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

Maybe you are looking for

  • Z3 Compact not able to do things while on call

    I've noticed with my Z3 Compact that it doesn't allow me to really do anything in a call. Like reply to a text, etc. Where as with other android devices, and apple products I have been able to do while doing same things. It allows you to get into mes

  • Oracle object cols and inserts

    hi, i have a type defined as follows create type foo a number(10), b number(10), c date, d date); now i use this in a table as follows create table test foo_obj foo default foo(-1, -1, sysdate, sysdate) not null, foo_name varchar2(100) not null now w

  • Clip/effect question

    i got imovie last week and i'm curious if it has this clip/effect.It starts from 0:00 - 0:09.I want to put pictures of my choice in the boxes as intros for a couple of videos i'm doing. http://youtube.com/watch?v=tbkmDisF6r0

  • Time stamps in reports and status are 7hrs off, is there a setting?

    Time stamps in reports and status are 7hrs off, is there a regional setting to correct this? When I run e-load all reports and any other time stamps are 7 hours off. (If I run a test at 9:20am pacific time the e-load shows 4:20pm). The e-test scripts

  • Macbook shutdown / freeze black screen and sound loop

    Hello ! For a few months now my macbook pro 2010 shuts down automatically after some times of working (sometime 2 minutes ... sometimes 2 hours !) Black screen If a sound was played, it looped Keyboard keep being lightened My macbook seems to freeze