turn data into columns

Hello

I would like to turn rows into columns and need your help

I have

Control Panel

where we have two columns
OrderID, orderdate
orderid     order_date
1234     9/10/2009
4555     10/1/2009
I have order_codes
have 3 columns
OrderID
/ / DESC
value

data to order _codes
orderid     desc     value
1234     ch_code1     5789
1234     ch_date     10/20/2009
1234     ch_miscval     678.9
1234     ch_charge     765
          
4555     ch_code1     9999
4555     ch_date     10/0/2009
4555     ch_miscval     78.9
now

I need to transform the data like this
orderid ==     ch_code1      ==ch_date ==     ch_miscval ==     ch_charge ==     order_date
1234 ==     5789 ==     10/20/200  ==    678.9 ==     765      == 9/10/2009
4555 ==     9999 ==     10/1/2009      == 78.9 ==     ---      == 10/1/2009
any help is appreciated

Thank you

Published by: vg November 2, 2010 09:47

Try this. Hope this helps

/* Formatted on 2010/11/02 13:29 (Formatter Plus v4.8.8) */
WITH order_desc AS
     (SELECT 1234 orderid, 'ch_code1' descp, '5789' VALUE
        FROM DUAL
      UNION ALL
      SELECT 1234, 'ch_date', '10/20/2009'
        FROM DUAL
      UNION ALL
      SELECT 1234, 'ch_miscval', '678.9'
        FROM DUAL
      UNION ALL
      SELECT 1234, 'ch_charge', '765'
        FROM DUAL
      UNION ALL
      SELECT 4555, 'ch_code1', '9999'
        FROM DUAL
      UNION ALL
      SELECT 4555, 'ch_date', '10/0/2009'
        FROM DUAL
      UNION ALL
      SELECT 4555, 'ch_miscval', '78.9'
        FROM DUAL),
     order_t AS
     (SELECT 1234 orderid, SYSDATE order_date
        FROM DUAL
      UNION ALL
      SELECT 4555, SYSDATE
        FROM DUAL)
SELECT   a.orderid, MAX (DECODE (descp, 'ch_code1', VALUE, NULL)) ch_code1,
         MAX (DECODE (descp, 'ch_date', VALUE, NULL)) ch_date,
         MAX (DECODE (descp, 'ch_miscval', VALUE, NULL)) ch_miscval,
         MAX (DECODE (descp, 'ch_charge', VALUE, NULL)) ch_charge,
         MAX (DECODE (descp, 'order_date', VALUE, NULL)) order_date
    FROM order_desc a, order_t b
   WHERE a.orderid = b.orderid
GROUP BY a.orderid
Output 

   ORDERID CH_CODE1   CH_DATE    CH_MISCVAL CH_CHARGE  ORDER_DATE
---------- ---------- ---------- ---------- ---------- ----------
      1234 5789       10/20/2009 678.9      765
      4555 9999       10/0/2009  78.9                            

Published by: new learning on November 2, 2010 10:30

Tags: Database

Similar Questions

  • Turn rows into columns

    Hello:

    I have a table as follows:

    Date, person, cost
    April 16, 2011, John, 10
    April 16, 2011, Sam, 20
    April 16, 2011, Owen, 39
    April 16, 2011, Rolf, 13
    April 17, 2011, John, 50
    April 17, 2011, Sam, 40
    April 17, 2011, Owen, 60
    April 17, 2011, Rolf, 25
    April 18, 2011, Sam, 55
    April 18, 2011, Owen, 31

    I need to write a query so that the output will be as follows:

    Date, Sam, Owen, John, Rolf
    APRIL 16, 2011, 10, 20, 39, 13
    APRIL 17, 2011, 50, 40, 60, 25
    APRIL 18, 2011, 0, 55, 31, 0

    Need help.

    Thank you-
    Tanvir

    Published by: 853856 on April 21, 2011 14:19

    Published by: 853856 on April 21, 2011 14:21

    Hello
    Welcome to the forum!

    Try this please

    WITH DATA AS
    (SELECT '16-APR-2011' DDATE, 'John' FULLNAME, '10' AMOUNT FROM DUAL UNION ALL
    SELECT '16-APR-2011', 'Sam', '20'  FROM DUAL UNION ALL
    SELECT '16-APR-2011', 'Owen', '39' FROM DUAL UNION ALL
    SELECT '16-APR-2011', 'Rolf', '13' FROM DUAL UNION ALL
    SELECT '17-APR-2011', 'John', '50' FROM DUAL UNION ALL
    SELECT '17-APR-2011','sam', '40' FROM DUAL UNION ALL
    SELECT '17-APR-2011', 'Owen', '60' FROM DUAL UNION ALL
    SELECT '17-APR-2011', 'Rolf', '25' FROM DUAL UNION ALL
    SELECT '18-APR-2011', 'Sam', '55' FROM DUAL UNION ALL
    SELECT '18-APR-2011', 'Owen', '31' FROM DUAL)
    
    SELECT DDATE,
      NVL(MIN (CASE WHEN FULLNAME='John' THEN AMOUNT END),0) JOHN,
      NVL(MIN (CASE WHEN FULLNAME='Sam' THEN AMOUNT END),0) SAM,
      NVL(MIN (CASE WHEN FULLNAME='Owen' THEN AMOUNT END),0) OWEN
     FROM DATA
     GROUP BY DDATE
    
  • How to turn rows into columns

    Suppose I have the following table:

    FIELD1 FIELD2 FIELD3
    a desc1 5
    a 10 desc2
    desc1 b 3
    b desc3 6
    desc4 c 2
    ...

    I would like to write a query, get the following result in order not to get a dulpication for Field1:

    FIELD1 DESC1 DESC2 DESC3 DESCS4
    a 10 5
    3 6 b
    a p
    ...

    Can you help me?
    Thanks in advance

    Mark

    Hello
    This link can help you:
    http://saadnayef.blogspot.com/2010/05/converting-rows-to-columns-and-Vice.html

    Saad,

  • Problem using applescript to put data into the table of numbers with column heads

    I have extracted the data from certain Web pages and want to place the data items in a table of numbers.  I wrote the applescript to extract pairs of data into two lists, but encountered a problem when you try to put the data items in a table of numbers.

    I hope I've isolated the problem eventually reduce version of the data table and writing.

    Calendar_Month

    Alvarez

    Laundry

    Linen

    Products

    March-2014

    April 2014

    May-2014

    Totals:

    0.00

    0.00

    0.00

    0.00

    Define theLabels to {"Alvarez", "Flax", "Laundry", "Products"}

    the nominative value {11, 22, 33, 44}

    Tell application "Numbers."

    say table 1 on sheet 1 of 1

    rowIndex Set of 3

    q Set of 2

    say the line rowIndex

    colHead theLabels point q value

    the columnIndex value address column colHead

    tell the cell (columnIndex)

    value defined in point q of the nominative case

    tell the end

    tell the end

    tell the end

    tell the end

    I arbitrarily chose to line 3 of the table to demonstrate the problem.

    Each data item is associated, due to its position in the list, with a label that determines the column of the data table, where it should be placed.

    The illustrated script pitches the 2nd data element in the list in the column headed "Lin".  This seems to work ok.  But if q is set to 1 the script fails with

    get address of column "Alvarez" in line 3 of table 1 to sheet 1 of the document 1

    -> error number - 1728 column "Alvarez" in line 3 of table 1 of sheet 1 of document 1

    It fails also with q the value 3 or 4.

    I probably did something really stupid, but I can't understand this behavior.  Advice please?

    Under: Numbers v.3.6.1 Script Editor 2.7 2.4 OS X 10.5.5 Applescript

    Two questions.

    (1) the order of the text in theLabels = {"Alvarez", "Flax", "Laundry", "Products"} and differ from the order of the text in the header row. Is this correct?

    (2) you are putting the value 22 (point 2 of nominative) in cell D3, where column heading of D = "Flax", which is article 2 of the theLabels?

    Respect,

    H

  • I tried to download a pdf and convert them into excel, but the data in excellent is always to the image format.  How can I get the pdf data into the columns and rows?

    I tried to download a pdf and convert them into excel, but the data in excellent is always to the image format.  How can I get the pdf data into the columns and rows so that I can do the calculations?

    If you start the https://forums.adobe.com/welcome Forums Index

    You will be able to select a forum for the specific Adobe products you use

    Click on the symbol "arrow down" on the right (where it is said to see all our products and Services) to open the drop-down list and scroll

  • How to insert data into a BLOB column using sql

    Hi all

    How to insert data into the BLOB column directly using sql.
    create  table temp
    (
      a blob,
      b clob);
    
    SQL> /
    Insert into temp  values ('32aasdasdsdasdasd4e32','adsfbsdkjf') ;
                                      *
    ERROR at line 1:
    ORA-01465: invalid hex number
    
    Please help in this.
    Thank you
    Prakash P

    Insert into temporary values (UTL_RAW. CAST_TO_RAW ('32aasdasdsdasdasd4e32'), 'adsfbsdkjf');

  • How to load several files column data into essbase using the rule of load.

    Hello

    I need to load a file of data into essbase, which includes data from several columns.

    Here is a sample file.


    Year, Department, account, Jan, Feb, Mar, Apr, may, June, July, August, Sept, Oct, Nov, Dec
    FY10, ministere1, account1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    FY10, agencies2, account1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    FY10, ministere3, account1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12


    Thank you
    Sirot

    But this isn't an error as such, that is to say that no data values have been changed so that they possible already exist in the database.
    If there is no release, they should be in a file of errors.

    See you soon

    John
    http://John-Goodwin.blogspot.com/

  • How to insert data into the BLOB column

    Hi all

    Can someone help me to insert data in the BLOB data type column?

    The structure of the table is
    CREATE TABLE XXATFL_DM_FORCAST_STG
    (
    TASK_ID NUMBER,
    USER_ID NUMBER,
    CREATED_BY NUMBER (15),
    CREATION_DATE DATE,
    LAST_UPDATED_BY NUMBER (15),
    DATE OF LAST_UPDATE_DATE,
    LAST_UPDATE_LOGIN NUMBER (15),
    RECORD_STATUS VARCHAR2 (1 BYTE),
    ERROR_MESSAGE VARCHAR2 (4000 BYTE),
    DATA_FILE BLOB
    )

    I want to insert data into the column DATA_FILE. and this insert statement inside a procedure.

    Please help me as soon as possible because it is very urgent for me

    Thank you and best regards,
    Charrier

    Charrier,

    If you form the string yourself, you can use the function utl_raw.cast_to_raw on your channel.

    http://download.Oracle.com/docs/CD/B12037_01/AppDev.101/b10802/u_raw.htm#997086

    sql> create table t(
      2    id number,
      3    l_blob blob
      4  );
    
    sql> insert into t values(1, utl_raw.cast_to_raw('SampleString'));
    
    1 row created.
    
    sql> commit;
    
    Commit complete.
    
  • display data in rows into columns

    Hi guys

    I have data from several lines and to display the same data in columns as several colors by product. color are set in numbers, that is to say only three colors are there in a product, no more than that...

    sample data is like this

    ID product_name color

    101 has Red

    101 has black

    101 has Green

    expected result: -.

    ID product_name colour1 2 Couleur3

    101 has red black Green

    Thanks and greetings

    Maybe...

    SQL > SELECT a.id, a.product_name,.

    2 a.first_colour, b.colour second_colour, a.third_colour

    3 (SELECT a.id, a.product_name,

    4 MIN (a.colour) first_colour,

    5 MAX third_colour (a.colour)

    6 table_name FROM one

    7 GROUP BY a.id, a.product_name) a, b from table_name

    8. WHERE the b.colour NOT IN (a.first_colour, a.third_colour)

    9 AND b.product_name = a.product_name

    10 AND a.id = b.id;

    ID P FIRST_ SECOND THIRD_

    ---------- - ------ ------ ------

    101 a black green red

    102 purple yellow orange B

    SQL >

  • Insert data into the same table based on certain conditions

    Hello. I'm new to this forum.
    I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:

    Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
    I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:

    1. if PaidAmount > 0 & & CurrentInstallment < TotalInstallment then

    CurrentInstallment = CurrentInstallment + 1

    2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...

    So, I have to insert data by incrementing the month and year. for example:

    If currentdata is 11/2012 next data will be 12/2012

    But following will be 01, 2013
    I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit on each record selected and insert data and then turns them into table (MYTABLE) even.

    How to achieve that?

    Thank you.

    978184 wrote:
    Hello. I'm new to this forum.
    I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:

    Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
    I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:

    1. if PaidAmount > 0 & CurrentInstallment

    CurrentInstallment = CurrentInstallment + 1

    2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...

    So, I have to insert data by incrementing the month and year. for example:

    If currentdata is 11/2012 next data will be 12/2012

    But following will be 01, 2013
    I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit & on each of the selected data record and insert then turns them into table (MYTABLE).

    You can do this way:

    This is not tested, but if you can provide the example of table structure and data (IN create table and insert scripts), it can be put to the test.

    insert into your_table
    (col1, col2, col3...current_installment, month_field)
    select col1, col2, col3..,
           current_installment +
           case when paidamount > 0 and current_installment < total_installment then
            row_number() over (
                                partition by column1, column2,.. columnn      -->You may choose partition if you want the
                                                                              --Increment of Current_installment to reset after particular combination ends
                                order by primary_key        -->Order the Increment, you may choose to add more columns to order by
                              )
          else
            0                                               --> if condition is not met, then Add 0
          end curr_installment,
          add_months(to_date(month_field, 'MM/YYYY'), 1) nxt_month
      from your_table;
    
  • Loading the data into the order of series of Timestamp

    I need to load data into the target table in the order of series of timestamp. How to do it.

    ex:

    2015-12-10 02:14:15

    2015-12-10 03:14:15

    2015-12-10 04:14:15

    After you follow the how to use the Direct-Path INSERT ordered by your "timestamp column" series described here above, you can sort the lines in ODI (order of) this way:

  • How to load data into the App MVDEMO schema example

    Hi all

    I'm a POC on Oracle Mapviewer and try to build some reports in OBIEE using MApviewer.

    This POC, I use Oracle MVDEMO example Data (11g). I think that these sample data covers few countries like the USA.

    I need to make PDS for the Brazil, I downloaded data from the map of the site as Shapefiles Brazil

    http://GADM.org/country

    in these data of the Brazil, I got from .csv files 4 extensions, .dbf, .shp and SHX

    I need to know how can I load these files into my Oracle 11 g DB? Should I load data into the same pattern of mvdemo, if yes then which table?

    Any help will be appreciated a lot.

    Thank you

    Amit

    Use the Java shapefile Converter utility (http://www.oracle.com/technetwork/database/options/spatialandgraph/downloads/index-093371.html)

    GDAL (gdal.org) FME (Safe) or or MapBuilder.

    Specify the to SRID (i.e. the SRID for loading in Oracle geoms) 4326 or 8307.

    Load into a new table named anything you want. for example brazil_gadm with the geometry named GEOMETRY column

    Once it's loaded, verify that there is an entry for the table and column (BRAZIL_GADM, GEOMETRY) in user_sdo_geom_metadata

    Create a space on brazil_gadm.geometry index if the tool has not created a.

    Add the definitions of topic for the country, State or whatever the admin areas exist in the dataset.

    Import them as layers in OBIEE.

  • Need a sql script loader to load data into a table

    Hello

    IM new to Oracle... Learn some basic things... and now I want the steps to do to load the data from a table dump file...

    and the script for sql loader

    Thanks in advance

    Hello

    You can do all these steps for loading data...

    Step 1:

    Create a table in Toad to load your data...

    Step 2:

    Creating a data file... Create your data file with column headers...

    Step 3:

    Creating a control file... Create your control file to load the data from the table data file (there is a structure of control file, you can search through the net)

    Step 4:

    Move the data file and the control file in the path of the server...

    Step 5:

    Load the data into the staging table using sql loader.

    sqlldr control = data =

    connect as: username/password@instance.

  • Insert data into the source of destinator Table

    Hi all

    Need to insert data in sample_table1 table xxc_source_table sample_table2

    create table xxc_source_table (DESCRIPTION varchar2 (10));

    Insert the table xxc_source_table values('A201.) ABC.4084.GR');

    create table sample_table1 (col_1 varchar2 (10), col_2 varchar2 (10), col_3 varchar2 (10), col_4 varchar2 (10));

    create table sample_table2 (col_1 varchar2 (10), col_2 varchar2 (10), col_3 varchar2 (10), col_4 varchar2 (10), moved_flag varchar2 (2));

    col_1 = A201

    col_2 = ABC

    col_3 = 4084

    col_4 = GR

    Note: Insert data into the col_1, the col_3, the col_4 of the xxc_source_table sample_table1

    (2) if the next (form xxc_source_table) data is exist in the sample_table2, and then set the moved_flag as Y in this column

    3) xxc_source_table has 17000 lines

    Thank you.

    Post edited by: Rajesh123 please do not consider cross the line message

    Hi Renon,

    Why you don't want substr and instr? For best performance, you should go with substr and instr instead of regexp_substr. However you asked me to provide the code instead of substr and InStr. Then try the below...

    INSERT ALL

    IN VALUES sample_table1 (col1, col2, col3, col4)

    IN sample_table2 VALUES (col1, col2, col3, col4, 'Y')

    SELECT REGEXP_SUBSTR (DESCRIPTION,'[^.] +', 1, 1) col1,.

    REGEXP_SUBSTR (DESCRIPTION,'[^.] +', 1, 2) col2.

    REGEXP_SUBSTR (DESCRIPTION,'[^.] +' 1, 3) col3.

    REGEXP_SUBSTR (DESCRIPTION,'[^.] +' 1, 4) col4

    OF xxc_source_table;

    Thank you

    Ann

  • How to insert data into the table of plain text stored as a CLOB in another table

    Hi people,

    Maybe it's an easy question for someone who is more frequently used files and CLOB.

    I have a table that stores text files in the CLOB column. Files text includes some basic data that I want to load in the other - a text line = a new insert for me. Is there a "trick" how to do it effectively?

    Here is my model:

    Table OS_IMPORT_DOCS, stores the complete files as CLOB
    SQL> desc OS_IMPORT_DOCS
    Name          Type           Nullable Default Comments 
    ------------- -------------- -------- ------- -------- 
    OBJECT_ID     NUMBER                                   
    DATUM_ZMENY   DATE                    sysdate          
    FILE_PATH     VARCHAR2(4000) Y                         
    FILE_NAME     VARCHAR2(4000) Y                         
    FILE_SIZE     NUMBER         Y                         
    LAST_MODIFIED DATE           Y                         
    DOCUMENT      CLOB           Y                         
    STATUS        VARCHAR2(15)   Y        'NEW'  
    Sample data from OS_IMPORT_DOCS
    SQL> select *
      2    from os_import_docs d
      3  order by d.last_modified desc
      4  ;
     
     OBJECT_ID DATUM_ZMENY FILE_PATH                      FILE_NAME       FILE_SIZE  LAST_MODIFIED DOCUMENT    STATUS
    ---------- ----------- ------------------------------ --------------- ---------- ------------- ----------- ---------------
       1815043 13.8.2012 1 d:\data\C120813.DAT            C120813.DAT          16800 13.8.2012 16: <<CLOB>>    NEW
       1815042 13.8.2012 1 d:\data\C120812.DAT            C120812.DAT           3600 12.8.2012 22: <<CLOB>>    NEW
       1815041 13.8.2012 1 d:\data\C120811.DAT            C120811.DAT           1800 11.8.2012 13: <<CLOB>>    NEW
    Example of file CLOB - stored text data (select d.document from os_import_docs d where d.object_id = 1815042 ;)
    061053120820120000AGT000002Osoby                   0000000042301000000017210632
    062322120820120000AGT000002Osoby                   0000000012301000000017197566
    063526120820120001AGT000002Osoby                   0000000012301000000017197566
    064234120820120001AGT000002Osoby                   0000000103301000000162218777
    Above the example text includes "columns" in plain text:
    timestamp - 1-14, SSMIHH24DDMMYYYY position format
    flag - post 15-18
    company code - position 19-27
    etc...

    How can I query data stored within the OS_IMPORT_DOCS. The DOCUMENT column, divide it into columns and insert into another table?
    I have to read this method of 'online' file?


    Thank you very much
    Tomas

    For the first three columns:

    SQL> create type TRecord is object (
      2    ts           timestamp
      3  , flag         varchar2(4)
      4  , company_code varchar2(9)
      5  );
      6  /
    
    Type created
    
    SQL>
    SQL> create type TRecordTable is table of TRecord;
      2  /
    
    Type created
    
    SQL>
    SQL> create or replace function parse_clob (p_doc in clob)
      2  return TRecordTable pipelined
      3  is
      4    lf      number;
      5    eol     varchar2(2) := chr(10);
      6    eollen  number := length(eol);
      7    line    varchar2(32767);
      8    offs    number := 1;
      9  begin
     10    loop
     11      lf := dbms_lob.instr(p_doc, eol, offs);
     12      if lf != 0 then
     13        line := dbms_lob.substr(p_doc, lf - offs + 1 - eollen, offs);
     14        offs := lf + eollen;
     15      else
     16        line := dbms_lob.substr(p_doc, dbms_lob.getlength(p_doc) - offs + 1, offs);
     17      end if;
     18      pipe row (
     19        TRecord(
     20          to_timestamp(substr(line, 1, 14), 'HH24MISSDDMMYYYY')
     21        , substr(line, 15, 4)
     22        , substr(line, 19, 9)
     23        )
     24      );
     25      exit when lf = 0;
     26    end loop;
     27  end;
     28  /
    
    Function created
    
    SQL>
    SQL> select t.*
      2  from os_import_docs d
      3     , table(parse_clob(d.document)) t
      4  where d.object_id = 1815042;
    
    TS                                     FLAG COMPANY_CODE
    -------------------------------------- ---- ------------
    12/08/12 06:10:53,000000               0000 AGT000002
    12/08/12 06:23:22,000000               0000 AGT000002
    12/08/12 06:35:26,000000               0001 AGT000002
    12/08/12 06:42:34,000000               0001 AGT000002
     
    

Maybe you are looking for

  • Mobi Office Suite Pro system does not start

    After that download OS 10 Office Suite Pro (Robi system) begins to start but immediately closes back to icons. With the help of the Ipad Air.

  • Does not receive new emails from thunderbird to my gmail account not

    HelloSince 4 February I get new emails in my thunderbird (version 31.4.0) to my gmail account. I can send emails, however.I have 2 other e-mail accounts and that they do not have the same problem. If I have access to my account gmail since the browse

  • display of images on ebay and amazon

    When I go to these sites Web there is no images displayed. The logos of sites are absent as are all images of products for sale.

  • How to remove a user name from the logon screen?

    Hi all I have an annoying problem and do not know how to solve. What happened is that used to use my laptop for the log-in het Skype account my ex-girlfriend. But in the login screen continues to display his name as mine always does. I used different

  • To replace my mini wifi adapter PCIe (half)

    Laptop HP dv6 6140tx help please buy a best of wifi adapters My lappy has the Ralink RT5390 802.11b/g/n WiFi Adapter, which malfunctioned and I want to replace it with a better which can receive the signal of a lot better. Also the adapter should be