Mother of all unpivots - columns to rows?

I have multiple tables as follows:
eventdate event1 event2 event3 event4 ... eventn
1/1/07 8 5 3 2 ... 7
2/1/07 1 14 6 9 ... 22
n/n
The tables have different column headers (apart from date), different numbers of columns and different numbers of rows.
I'd like to copy the data from these tables to one table as follows:
tablename eventname eventdate count
table1 event1 1/1/07 8
table1 event2 1/1/07 5
etc
Can anyone advise a solution? Something that handled one source table and could be set up to cycle through multiple tables via script would be great.
I have hit google and forums hard and seen a lot of solutions that either hard code column names or parse the contents of a delimited data column. This is not what I'm after.
Thanks in advance
user592258

Prior to 11g, unpivot goes like this:
SELECT id, column_value
FROM   your_table t1
     , TABLE(your_collection_type(col1,col2,col3,col4)) t2;which transforms
id, col1, col2, col3, col4into
id  column_value
id  column_value
id  column_value
id  column_valueIt gets a little more complicated if you have to generate a second column to show which source column the value was taken from. You would need an object type with two attributes and a collection of that type, and then use something like
SELECT id, t2.label, t2.val
FROM   your_table t1
     , TABLE(your_collection_type
       ( your_object_type(1,col1)
       , your_object_type(2,col2)
       , your_object_type(3,col3)
       , your_object_type(4,col4))
       ) t2;

Similar Messages

  • Need help on Unpivot (Columns to rows conversion)

    Could you please help me to do unpivot using sql?
    Table creation and insertion scripts:
    create table REQ
    ID NUMBER,
    VALUE VARCHAR2(20)
    insert into REQ (ID, VALUE)
    values (1, 'HI,HELLO, KARTHI');
    insert into REQ (ID, VALUE)
    values (2, 'ARE,YOU,FINE,BHAR');
    insert into REQ (ID, VALUE)
    values (3, '100,200,300');
    commit;
    I need to view the data in the below format
    ID VALUE
    1 HI
    1 HELLO
    1 KARTHI
    2 ARE
    2 YOU
    2 FINE
    2 BHAR
    3 100
    3 200
    3 300

    However, I would take care on the performance issue.So let's use the model clause:
    SQL> set autotrace on
    SQL> select id
      2       , v
      3    from req
      4   model
      5         partition by (id)
      6         dimension by (0 i)
      7         measures (value v)
      8         rules iterate (10) until (instr(v[iteration_number+1],',') = 0)
      9         ( v[iteration_number+1] = substr(v[iteration_number],instr(v[iteration_number],',')+1)
    10         , v[iteration_number] = substr(v[iteration_number],1,instr(v[iteration_number],',')-1)
    11         )
    12   order by id
    13       , i
    14  /
                                        ID V
                                         1 HI
                                         1 HELLO
                                         1  KARTHI
                                         2 ARE
                                         2 YOU
                                         2 FINE
                                         2 BHAR
                                         3 100
                                         3 200
                                         3 300
    10 rijen zijn geselecteerd.
    Uitvoeringspan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3 Card=3 Bytes=75)
       1    0   SORT (ORDER BY) (Cost=3 Card=3 Bytes=75)
       2    1     SQL MODEL (ORDERED FAST) (Cost=3 Card=3 Bytes=75)
       3    2       TABLE ACCESS (FULL) OF 'REQ' (TABLE) (Cost=2 Card=3 By
              tes=75)
    Statistics
             48  recursive calls
              0  db block gets
             11  consistent gets
              0  physical reads
              0  redo size
            528  bytes sent via SQL*Net to client
            271  bytes received via SQL*Net from client
              6  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
             10  rows processedRegards,
    Rob.

  • Avoiding zeros in columns and rows after selecting parameters

    Hi,
    I have a parameter on my report, after selecting certain value it shows me Zero Zero for all the columns and rows that do not satisfy the parameter selected and shows data in rows nad columns whre it is matching.
    Is there any way to just avaid getting anything for the rows and columns that are not matching and just getting data rows and columns......like a compact form.
    I hope I was able to explain my question.
    Robinn

    Please re-post if this is still an issue or purchase a case and have a dedicated support engineer work with you directly:
    http://store.businessobjects.com/store/bobjamer/DisplayProductByTypePage&parentCategoryID=&categoryID=11522300?resid=-Z5tUwoHAiwAAA8@NLgAAAAS&rests=1254701640551

  • Columns To Rows Transpose

    Need some help to display the output data in the below format,Pls help out...
    Eg:
    OV:Old Value
    NV:New Value
    ID OV NV
    1 -- 100
    1 100 200
    1 200 300
    OUTPUT:
    ID VALUES
    1 100 200 300

    Hi,
    i think this link helpful to you
    http://www.club-oracle.com/forums/unpivoting-column-to-row-conversion-techniques-sql-t145/

  • Eliminating Columns and Rows

    I have a spreadsheet. Latitudes are across row 1. Latitudes are across row A. The
    lower case letters are values of some function. I would like to delete all the columns and rows in the spreadsheet except for those that are closest to .00, .25,.50, .75.
    For example, this
    A
    B
    C
    D
    E
    1
    -91.5125
    -91.5042
    -91.4959
    -91.4875
    2
    42.52124
    a
    b
    c
    d
    3
    42.5129
    e
    f
    g
    h
    4
    42.50457
    i
    j
    k
    l
    5
    42.49624
    m
    n
    o
    p
    would turn out to this...
    A
    B
    1
    -91.4959
    2
    42.49624
    o
    How would I go about doing so?

    Thanks Jerry,
    sure wish this was easy to access in the help menu or on the web. not burried deep in a huge manual that is burried deep in the website. 
    For others who might read this, the corner square is called a "Table Handle"
    The A, B, C row is called "Reference tab letters" and can be used to refer to columns.
    The 1, 2, 3 column is called "Reference tab numbers" and can be used to refer to rows.
    http://manuals.info.apple.com/en_US/Numbers09_UserGuide.pdf

  • Transpose of columns to rows (unpivoting) and not rows to columns

    Hi,
    I am right now using oracle 10g. I HAVE the following specification. Here I specified only 5 columns.
    We have upto 200 columns.
    TRANS_ID      PORTFILIO_NUM     TICKER          PRICE     NUM_SHARES ........................................
    2     100     MO      25.00     100 ........................................
    3     100     MCD          31.50     100 ........................................
    I want the above to be transformed into the following output .
    TRANS_ID TYPE VALUE
    2 PORTFILIO_NUM 100
    2 TICKER MO
    2 PRICE 25.00
    2 NUM_SHARES 100.
    I don't want to use case/decode function by hard coding the 200 columns.
    Can anyone provide me a good way (mostly dynamic way) of doing this?
    I searched the whole forum and also other forums. Everywhere I could find
    rows to columns / columns to rows where the column names have been hardcoded.
    I want a dynamic way of doing it. Let me know if u need any other inputs.
    DDL :
    CREATE TABLE PORT_TRANS
    TRANS_ID VARCHAR2(100 BYTE),
    PORTFILIO_NUM VARCHAR2(100 BYTE),
    TICKER VARCHAR2(100 BYTE),
    PRICE VARCHAR2(100 BYTE),
    NUM_SHARES VARCHAR2(100 BYTE)
    INSERT INTO PORT_TRANS (TRANS_ID,PORTFILIO_NUM,TICKER,PRICE,NUM_SHARES)
    VALUES('2','100','MO','25.00','100');
    INSERT INTO PORT_TRANS (TRANS_ID,PORTFILIO_NUM,TICKER,PRICE,NUM_SHARES)
    VALUES('3,'100','MCD','31.50','100');
    COMMIT;
    Thanks,
    Priya.

    Hi,
    What you're trying to write is something like this:
    WITH     cntr     AS
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <= 4
    SELECT     p.trans_id
    ,     CASE
              WHEN  c.n     <= 2
              THEN
                   CASE     c.n
                        WHEN 1     THEN 'PORTFILIO_NUM'
                        WHEN 2     THEN 'TICKER'
                   END
              ELSE
                   CASE     c.n
                        WHEN 3     THEN 'PRICE'
                        WHEN 4     THEN 'NUM_SHARES'
                   END
         END     AS type
    ,     CASE
              WHEN  c.n     <= 2
              THEN
                   CASE     c.n
                        WHEN 1     THEN p.PORTFILIO_NUM
                        WHEN 2     THEN p.TICKER
                   END
              ELSE
                   CASE     c.n
                        WHEN 3     THEN p.PRICE
                        WHEN 4     THEN p.NUM_SHARES
                   END
         END     AS value
    FROM          port_trans     p
    CROSS JOIN     cntr          c
    ORDER BY     p.trans_id
    ,          c.n
    ;I wrote this as if CASE could only handle 2 choices, rather than 128, just to show how to nest CASE expressions.
    What you have to do is write the CASE expressions, based on the contents of all_tab_columns.
    In your sample data, all of the columns are VARCHAR2 (another design flaw). If you have any columns of other types, use TO_CHAR to convert them to VARCHAR2; that is, the final code to be run will have something like:
    ...                    WHEN 4     THEN TO_CHAR (p.NUM_SHARES)If I had to do this, I might run several queries on all_tab_columns, each producing one script, containing just a fragment of the query.
    To run the whole thing, I would hard-code a main query like this
    WITH     cntr     AS
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <=
                        @num_columns.sql
    SELECT     p.trans_id
    ,     CASE
              @type.sql
         END     AS type
    ,     CASE
              @value.sql
         END     AS value
    FROM          port_trans     p
    CROSS JOIN     cntr          c
    ORDER BY     p.trans_id
    ,          c.n
    ;As with any coidng, start small and take baby steps. Maybe the first step would just be to write num_columns.sql, which just contains the number 4. When you can do that, hard-code the CONNECT BY query, calling num_columns.sql.
    Good luck!

  • Update all null columns value in single row statement

    4) Write a single SQL statement to update all the columns of a table to have -9 instead of null value
    e.g:
    Before update:
    C1     C2     C3
    null     9     null
    10     null     88
    12     8     0
    After update:
    C1     C2     C3
    -9     9     -9     
    10     -9     88
    12     8     0
    Thanks

    Hi,
    Update < table name >
    set column name = nvl(column name, -9),
    column name = nvl(column name, -9)
    Regards
    Message was edited by:
    Seshu

  • 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                                                   

  • Column to Row Display

    Hi,
    I have 252 column table data, where in I need to display some of those columns values as rows in my report.
    col1 col2 col3 col4 .... col252
    i want to display this as
    row1
    row2
    row3
    row252.
    I am unable to use Pivot as I am using Oracle 10g. Pls suggest a solution on this?

    Hi,
    Always post your Oracle version. Don't just add an easy-to-miss tag like "10G" to your message; say something clear, like "I'm using Oracle 10.2.0.4.0".
    This is called Unpivot . One way to do it is to cross-join your table to a Counter Table , any table (or sub-query, as in the example below) that has as many rows as you need in your output.
    To unpivot all 252 columns:
    WITH     cntr     AS
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <= 252
    SELECT     c.n
    ,     CASE
             WHEN  c.n <= 100
             THEN  CASE c.n
                   WHEN   1  THEN     x.col_1
                   WHEN   2  THEN  x.col_2
                   WHEN 100  THEN  x.col_100
                END
             WHEN  c.n <= 200
             THEN  CASE c.n
                   WHEN 101  THEN     x.col_101
                   WHEN 102  THEN  x.col_102
                   WHEN 200  THEN  x.col_200
                END
             WHEN  c.n <= 300
             THEN  CASE c.n
                   WHEN 201  THEN     x.col_201
                   WHEN 202  THEN  x.col_202
                   WHEN 252  THEN  x.col_252
                END
         END     AS data_col
    FROM          cntr     c
    CROSS JOIN     table_x     x
    ;The nested CASE expressions are necessary because you can't have more than 127 WHEN-THEN pairs in a single CASE expression.
    If you're choosing 60 columns out of the 252 to unoivot, then you only need one CASE expression.

  • ROWS to COLUMNS and COLUMNS to ROWS (Earlier Docs/Threads Didn't help)

    Hi Guys,
    I have gone through the earlier threads and documents on this. But didn't help.
    In lots of cases they have used DECODE statement. However, it works if you know the number of columns and rows...what if we are not sure of the number of records? How does it work?
    Please direct me to any thread or any other documents.
    -Sandeep

    Pivoting in SQL
    a. 10g Model Clause
    SQL> create table test(id varchar2(2), des varchar2(4), t number);
    Table created
    SQL> INSERT INTO test values(’A',’a1',12);
    1 row inserted
    SQL> INSERT INTO test values(’A',’a2',3);
    1 row inserted
    SQL> INSERT INTO test values(’A',’a3',1);
    1 row inserted
    SQL> INSERT INTO test values(’B',’a1',10);
    1 row inserted
    SQL> INSERT INTO test values(’B',’a2',23);
    1 row inserted
    SQL> INSERT INTO test values(’C',’a3',45);
    1 row inserted
    SQL> commit;
    Commit complete
    SQL> SELECT * FROM test;
    ID DES T
    A a1 12
    A a2 3
    A a3 1
    B a1 10
    B a2 23
    C a3 45
    6 rows selected
    SQL> select distinct i, A1, A2, A3
    2 from test c
    3 model
    4 ignore nav
    5 dimension by(c.id i,c.des d)
    6 measures(c.t t, 0 A1, 0 A2, 0 A3)
    7 rules(
    8 A1[any,any] = t[cv(i),d = ‘a1'],
    9 A2[any,any] = t[cv(i),d = ‘a2'],
    10 A3[any,any] = t[cv(i),d = ‘a3']
    11 );
    I A1 A2 A3
    C 0 0 45
    B 10 23 0
    A 12 3 1
    SQL> select distinct d, A, B, C
    2 from test c
    3 model
    4 ignore nav
    5 dimension by(c.id i,c.des d)
    6 measures(c.t t, 0 A, 0 B, 0 C)
    7 rules(
    8 A[any,any] = t[i = ‘A’, cv(d)],
    9 B[any,any] = t[i = ‘B’, cv(d)],
    10 C[any,any] = t[i = ‘C’, cv(d)]
    11 );
    D A B C
    a1 12 10 0
    a3 1 0 45
    a2 3 23 0
    b. Pivoting INSERT example;
    INSERT ALL
    INTO sales_info VALUES (employee_id,week_id,sales_MON)
    INTO sales_info VALUES (employee_id,week_id,sales_TUE)
    INTO sales_info VALUES (employee_id,week_id,sales_WED)
    INTO sales_info VALUES (employee_id,week_id,sales_THUR)
    INTO sales_info VALUES (employee_id,week_id, sales_FRI)
    SELECT EMPLOYEE_ID, week_id, sales_MON, sales_TUE,sales_WED, sales_THUR,sales_FRI
    FROM sales_source_data;
    c. 11g sql pivot keyword
    http://oraclebizint.wordpress.com/2007/09/05/oracle-11g-pivot-and-unpivot/
    d. reference for others :)
    http://laurentschneider.blogspot.com/2005/08/pivot-table.html

  • Return all the column values using the F4IF_INT_TABLE_VALUE_REQUEST

    Hi,
    How to return all the column values using the F4IF_INT_TABLE_VALUE_REQUEST?
    For example : if the row has 3 columns then after selecting the particular row, the RETURN_TAB internal table should contain all the three column values.
    Regards,
    Raghu

    Hi,
       Try the following...
    DATA : it_fields like help_value occurs 1 with header line.
    data: begin of w_vbap,
            vbeln      like vbap-vbeln,    
            posnr      like vbap-posnr,   
            werks      like vbap-werks,  
          end of w_vbap.
    data: i_vbap   like w_vbap   occurs 0 with header line,
             w_fields type help_value,
          i_dfies type table of dfies,
          i_return_tab type table of ddshretval with header line,
          i_field  type dfies.
      select vbeln posnr werks
          from vbap into table i_vbap up to 5 rows.
    if sy-subrc = 0.
       sort i_vbap by vbeln.
    endif.
      clear it_fields[] , it_fields.
      it_fields-tabname = c_vbap.
      it_fields-fieldname = 'VBELN'.
      it_fields-selectflag = c_on.
      append it_fields.
      it_fields-tabname = c_vbap.
      it_fields-fieldname = 'POSNR'.
      it_fields-selectflag = space.
      append it_fields.
      it_fields-tabname = c_vbap.
      it_fields-fieldname = 'WERKS'.
      it_fields-selectflag = space.
      append it_fields.
      loop at it_fields into w_fields.
        i_field-tabname   = w_fields-tabname.
        i_field-fieldname = w_fields-fieldname.
        i_field-keyflag   = w_fields-selectflag.
        append i_field to i_dfies.
      endloop.
      call function 'F4IF_INT_TABLE_VALUE_REQUEST'
        exporting
          retfield               = 'VBELN'
         window_title           = 'Select'
        tables
          value_tab              = i_vbap
          field_tab                = i_dfies
          return_tab             = i_return_tab
       exceptions
         parameter_error        = 1
         no_values_found        = 2
         others                 = 3
      read table i_return_tab into w_return_tab index 1.
      if sy-subrc = 0.
      endif.
    Regards,
    Srini.

  • Interactive Report - Icon View - Dynamic Columns per Rows ?

    Hi all,
    We use the icon view functionnality in Interactive Report.
    Is there a way to display the 'columns per row' attribute as an application item and set it dynamical via PL/SQL ?
    Any suggestions?
    Thanks in advance for advices,
    Regards,
    Grégory

    Hi,
    Apex 4.0 interactive reports and images (Scott's thread)
    Have some useful information and pointers to the solution you are looking for.
    I hope this help.
    Thank you,
    Ranish

  • SWAP COLUMNS AND ROW IN AN INTERNAL TABLE to display in ALV

    Hi ,
    I want to swap all the rows in an internal table with the column of the internal table to display it horizontally in ALV grid.
    e.g
    1     2   3  (field names)
    A    P   X
    B    Q   Y
    C    R    Z
    should look like :
    D       A   B     C
    E      P   Q    R
    F       X    Y    Z
    Where D , E, F in first column is already apended in new table.
    Or else is there a way to rotate the ALV grid so that it can display rows as columns & columns as rows.
    regards

    hi,
    i have an internal table which is like
    f1  f2 f3  f4 (column header)
    A  1  2   3
    B  4  5   6
    C  7  8   9
    the values in o/p table should be
    A B C  ( column header)
    1  4 7 
    2  5 8
    3  6 9
    Please help!!

  • Filling in values based on the column and row headers

    I have a question that seems simple, but I can't figure out how to do it and I've searched all over the forum to no avail.
    I have one column that is width increasing by 3 in. increments (column A)
    I have one row that is height increasing by 3 in. increments (row B)
    I have one cell that is price (cell A1)
    I have a formula to calculate price per sq. ft.: =CEILING(A3B2/144,1)A1. This gives me the whole sq. ft. number multiplied by the price per sq. ft. to give me a total price at that dimension (A3 is the width, B2 is the height, and A1 is the cell that contains the price per sq. ft.).
    *Here is the problem*: When I try to cut and paste or fill the formula to the other cells, it doesn't calculate the formula as =CEILING(column A * Row B/144,1)*A1. Instead, it moves all of the values relative to the new cell that the formula is being pasted or filled to.
    Is there a way to autofill the cells so that the formula continues to refer to the width column, the height row, and the price cell?

    Aha! Apparently I didn't do enough research before I posted and I have since answered my own question. Here's how you do it. Look for "Distinguishing Absolute and Relative Cell References" and you should be able to find what you need. In my case, here is the formula I needed to have to make the copy and paste function do what I needed it to do: =CEILING($A3*B$2/144,1)*$A$. Hope that helps anyone who may have had the same problem I did.

  • How to display single dimension members in Columns and rows

    Item category on rows Item brand on columns, Different levels in the Item hierarchy , can it be presented as above in the pivot table ?
    I have Item dimension with the below hierarchy :
    Item
    |- All Items
    |-- CAT-Beauty Products
    | |-- SEG-Skin Care
    | |-- BD-Andes
    | -- CAT-Beverages
    | |-- SEG-Bottled Water
    | |--BD-Agua Vide
    | -- CAT-Grocery
    | |-- SEG-Meats
    | |- BD-Harris
    I need to display All 'CAT' elements in rows and all its children (SEG and BD elements) in columns in a pivot table.
    Could you please help me to form the MDX query to display the data in ADF Pivot table.
    Sample query that I used:
    SELECT
    {[Item].[CAT-Beauty Products].Siblings}
    ON COLUMNS,
    {[CAT-Beauty Products].members}
    ON ROWS
    FROM Vcp_Tu1.Supply
    This is giving the below error message:
    Statement Executed with Warnings.
    Repeated dimension [Item] in MDX Query
    Unexpected Essbase error 1200549
    Edited by: Swathi on Jun 7, 2011 11:42 PM

    Sorry you can't do that. as a work around you could create attribute dimensions for the parent and the could cposstab them, or split the dim into two dimensions

Maybe you are looking for