Oracle row to column transformation

RDBMS :10.2
Hi,
I have a query that composed of there tables and have joins in them. The final result query is giving is as folllow
ProductId ComProductId description
055X     035X     Ladies Companion
055X     055X     Adult Companion
112008     112009     Large Companion
112008     112008     Medium Companion
112009     112009     Medium Companion
112009     112008     Large Companion
I want to transform the output in following way, so every product may have its comProductID + Description in single row e.g. Product -ComProductId1 description1 ComProductId2 description2 .....
ProductId ComProductId description ComProductId description
055X     035X     Ladies Companion 055X     Adult Companion
112008     112009     Large Companion 112008     Medium Companion
Do we have any built in function or way to achieve this goal ?

thank you Hoek. it was really great to read. But as per post, i need to use "dynamic Pivot" but it is in 11g and i am using 10 g :(
Following query I am using to get results and that query I wan to transform
SELECT mainitem.productid as ProductId,
compitem.productid as ComProductId,
comp.header as description
FROM comp
JOIN item mainitem ON comp.Id = mainitem.id
JOIN item compitem ON comp.Companion = compitem.id;
this query produce following results
ProductId ComProductId description
055X 035X Ladies Companion
055X 055X Adult Companion
112008 112009 Large Companion
112008 112008 Medium Companion
112009 112009 Medium Companion
112009 112008 Large Companion
I want to transform the output in following way, so every product may have its comProductID + Description in single row e.g. Product -ComProductId1 description1 ComProductId2 description2 .....
ProductId ComProductId description ComProductId description
055X 035X Ladies Companion 055X Adult Companion
112008 112009 Large Companion 112008 Medium Companion
thanks

Similar Messages

  • Oracle rows to columns/pivot function

    Does anyone have any experience using the oracle pivot function? I have two tables:
    Table1:Users
    Username
    jsmith
    jjohnson
    jbeck
    Table2:Job Codes
    Username,Job Code
    jsmith,JC1
    jsmith,JC2
    jsmith,JC3
    jbeck,JC2
    I'm looking to formulate a query that will allow me to join the two tables and at the same time have the rows from table2 represented as columns (assuming a max of 3 job codes):
    Username,JobCode1,JobCode2,JobCode3
    jsmith,JC1,JC2,JC3
    jbeck,JC2,,
    It seems like pivot is the function I want to use for this but I can't seem to correlate the examples online back to what we're trying to do.

    Hi,
    Welcome to the forum!
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.
    See the forum FAQ {message:id=9360002}
    Since I don;t have a version of your table, I'll use the scott.emp table.
    I think you want something like this:
    WITH     got_r_num     AS
         SELECT     job, ename
         ,     ROW_NUMBER () OVER ( PARTITION BY  job
                                   ORDER BY          ename
                           ) AS r_num
         FROM    scott.emp
    SELECT       *
    FROM       got_r_num
    PIVOT       (    MIN (ename)
                FOR r_num IN ( 1      AS name_1
                                 , 2  AS name_2
                       , 3  AS name_3
    ORDER BY  job
    ;This shows up to 3 names from each job
    JOB       NAME_1     NAME_2     NAME_3
    ANALYST   FORD       SCOTT
    CLERK     ADAMS      JAMES      MILLER
    MANAGER   BLAKE      CLARK      JONES
    PRESIDENT KING
    SALESMAN  ALLEN      MARTIN     TURNERIf there are more than 3 rows with the same job, only the first 3 (in alphabetic order; that's what the analytic ORDER BY clause is doing) will be shown. For example, there's another SALESMAN named WARD who is not shown above.
    This query requires Oracle 11.1 (or higher).
    For other pivot techniques, including ones for earlier versions, see the forum FAQ {message:id=9360005}
    Edited by: Frank Kulash on May 25, 2012 10:20 AM
    Added output

  • Row to Column Transformation for Millions of records

    Hi Members,
    I need to transform data from two stage tables which has data in PIM structure (data stored as separate records ) to target table which has a flat structure (data stored in a single record). One of the stg tables has data volume of 45M and other has 5M records.The challenge I am seeing here is to transform such huge data into single table with considerable performance.What would be the ideal way to transform such huge data?Also can we have multiple programs running at the same time to achieve transformation for such huge data load quicker?
    Just to add my Oracle Version is 10g.
    Thanks
    Edited by: Sonic on Jul 12, 2010 1:33 AM

    Still no version number, still no code, and no explain plan report.
    Is there a better, faster way to do it?, I don't know ... how could I or anyone else as you've not told us what you are doing beyond the level of "my car won't start tell me why?"
    This should help you understand the issue from my keyboard.
    http://www.builderau.com.au/strategy/developmentprocess/soa/How-developers-should-ask-for-help/0,339028278,339299138,00.htm

  • Row to Column transformation on an update form

    I'm trying to create an updateable HTML-DB application that does the following. Any idea, the best way to do this?
    Converts table rows into HTML-DB columns.
    Create table INV_ORDERS
    (Month date,
    Beg_Inv number,
    Planned_orders number
    select * from INV_ORDERS;
    Month Beg_Inv Planned_Orders
    NOV-2005 10 20
    DEC-2005 30 40
    JAN-2006 50 60
    How can I pivot it to be:
    NOV-2005 DEC-2006 JAN-2006
    Beg_Inv 10 30 50
    Planned_Orders 20 40 60
    The pivot numbers need to be updatable (via HTML-DB).
    Thank you.

    As it stands, your problem definition is quite "hard". Each column in the HTML DB tabular form belongs to a different physical row. You could try creating the tabular form off of a view that does the pivot and writing a INSTEAD OF trigger on that view, but it is not going to be easy.
    But why?
    What problem are you trying to solve, maybe we can suggest a different approach?

  • Need a query to do row to column transformation

    SELECT friday_date FROM t2;Table Data:
    1/2/2009
    1/9/2009
    1/16/2009.......
    I need a query to get the output in below fashion
    Column
    1/2/2009,1/9/2009,1/16/2009
    I tried PIVOT and TRANSPOSE, cannot do this. I believe because it is a date,may be.
    I tried below code also, but CONNECT_BY_ISLEAF wont work on "ORA-00904: "CONNECT_BY_ISLEAF": invalid identifier"
    SELECT ltrim(sys_connect_by_path(FRIDAY_DATE,','),',') FRIDAY_DATE
           FROM (
            SELECT row_number() OVER(ORDER BY FRIDAY_DATE) rno,
                   FRIDAY_DATE
              FROM t2
          WHERE CONNECT_BY_ISLEAF = '1'
          start WITH rno = '1'
        connect BY rno = PRIOR rno+1;Thank You

    SQL>  WITH t2 AS (      SELECT TO_CHAR (
                                              NEXT_DAY (
                                                   DATE '2009-01-01' + (LEVEL - 1) * 7,
                                                   'friday'
                                              'mm/dd/yyyy'
                                              friday_date
                                  FROM DUAL
                        CONNECT BY LEVEL <= (DATE '2009-12-31' - DATE '2009-01-01') / 7)
    SELECT RTRIM (
                    XMLAGG (XMLELEMENT (
                                       e,
                                       friday_date || ','
                                  )).EXTRACT ('//text()'),
                    COLUMN_VALUE
      FROM t2
    COLUMN_VALUE                                                                   
    01/02/2009,01/09/2009,01/16/2009,01/23/2009,01/30/2009,02/06/2009,02/13/2009,02/
    20/2009,02/27/2009,03/06/2009,03/13/2009,03/20/2009,03/27/2009,04/03/2009,04/10/
    2009,04/17/2009,04/24/2009,05/01/2009,05/08/2009,05/15/2009,05/22/2009,05/29/200
    9,06/05/2009,06/12/2009,06/19/2009,06/26/2009,07/03/2009,07/10/2009,07/17/2009,0
    7/24/2009,07/31/2009,08/07/2009,08/14/2009,08/21/2009,08/28/2009,09/04/2009,09/1
    1/2009,09/18/2009,09/25/2009,10/02/2009,10/09/2009,10/16/2009,10/23/2009,10/30/2
    009,11/06/2009,11/13/2009,11/20/2009,11/27/2009,12/04/2009,12/11/2009,12/18/2009
    ,12/25/2009                                                                    
    1 row selected.

  • Convert rows to Columns in Oracle

    Hi,
    The table like
    Trx         Date
    PO121 23/11/2008
    PO122 24/11/2008
    PO123 25/11/2008
    I want to convert all the rows to columns like the below table
    PO121    23/11/2008    PO122    24/11/2008       PO123     25/11/2008
    Is it possible in Oracle ?

    Like this
    SQL> WITH T
      2  AS
      3  (
      4     SELECT 'PO121 23/11/2008' VAL FROM DUAL
      5     UNION ALL
      6     SELECT 'PO122 24/11/2008' FROM DUAL
      7     UNION ALL
      8     SELECT 'PO123 25/11/2008' FROM DUAL
      9  )
    10  SELECT MAX(DECODE(RNO,1,VAL)) VAL1, MAX(DECODE(RNO,2,VAL)) VAL2, MAX(DECODE(RNO,3,VAL)) VAL3
    11    FROM (SELECT ROW_NUMBER() OVER(ORDER BY VAL) RNO, VAL
    12       FROM T)
    13  /
    VAL1             VAL2             VAL3
    PO121 23/11/2008 PO122 24/11/2008 PO123 25/11/2008But beware the number of column must be known. without that you cant do it in a static SQL.
    Edited by: Karthick_Arp on Nov 10, 2008 1:41 AM

  • Converting Rows into Column in Oracle 10g

    Hi All,                    
    I m using Oracle Version 10.1.0.2.0 - Production                    
    I have requirement to convert rows into column wise as per the following:                    
    My Query is:                    
    WITH t                    
    AS ( SELECT 'A' AS x, 100 AS y FROM DUAL                     
    UNION ALL                    
    SELECT 'B',200 FROM DUAL                    
    SELECT X, Y                    
    FROM t;     
    X Y
    A 100
    B 200
    My Requirement is
    A B
    100 200
    So any one could help me that how I resolve this.
    Regards,
    Prasanta

    Dear frank,
    Thanks for your support,.
    It's working fine for static cases.If the first column is dynamic then how come i will resolve it.
    Example:
    Create table mytab (ID_C Varchar2(15),Value_N Number);
    Records Population into MyTab table is dynamic.
    Insert into mytab values('HO',5000);
    Insert Into mytab values('PG1',2400);
    Insert Into mytab values('PG2',3000);
    Insert Into mytab values('PG3',800);
    Commit;
    SQL> Select * From MyTab;
    IDC_ ValueN_
    HO 5000
    PG1 2400
    PG2 3000
    PG3 800
    Then My expected result will be as follows
    HO PG1 PG2 PG3
    5000 2400 3000 800
    Thanks and Regards,
    Prasanta

  • Row to Column XSL Transform in BLS

    I have 11.5, sr3.
    I was going to use the XSLTransformation action to swap rows and columns of a data set using /Illuminator/stylesheets/RowToColumnTransform.xsl. I cannot get anything but the following as an output:
    <?xml version="1.0" encoding="UTF-8"?><Rowsets DateCreated="2007-12-12T13:27:29" EndDate="2007-12-03T08:09:17" StartDate="2007-12-03T08:09:17" Version="11.5.3"><Rowset><Columns/><Row/></Rowset></Rowsets>
    There are no errors, I just don't get the result. The input data set is as follows:
    <?xml version="1.0" encoding="UTF-8"?><Rowsets DateCreated="2007-12-12T13:27:29" EndDate="2007-12-03T08:09:17" StartDate="2007-12-03T08:09:17" Version="11.5.3"><Rowset><Columns><Column Description="" MaxRange="1" MinRange="0" Name="User_ID" SQLDataType="1" SourceColumn="User_ID"/><Column Description="" MaxRange="1" MinRange="0" Name="User_Name" SQLDataType="1" SourceColumn="User_Name"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_Number" SQLDataType="4" SourceColumn="Sample_Number"/><Column Description="User Name" MaxRange="1" MinRange="0" Name="Login_By" SQLDataType="1" SourceColumn="Login_By"/><Column Description="Examination Type" MaxRange="1" MinRange="0" Name="Examination" SQLDataType="1" SourceColumn="Examination"/><Column Description="" MaxRange="1" MinRange="0" Name="Examination_Title" SQLDataType="1" SourceColumn="Examination_Title"/><Column Description="" MaxRange="1" MinRange="0" Name="Examination_Desc" SQLDataType="1" SourceColumn="Examination_Desc"/><Column Description="" MaxRange="1" MinRange="0" Name="Test_Number" SQLDataType="4" SourceColumn="Test_Number"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_Time" SQLDataType="93" SourceColumn="Sample_Time"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_Status" SQLDataType="1" SourceColumn="Sample_Status"/><Column Description="" MaxRange="1" MinRange="0" Name="Authorize" SQLDataType="1" SourceColumn="Authorize"/><Column Description="" MaxRange="1" MinRange="0" Name="Total_Defects" SQLDataType="4" SourceColumn="Total_Defects"/><Column Description="" MaxRange="1" MinRange="0" Name="Carton_Code_Date" SQLDataType="1" SourceColumn="Carton_Code_Date"/><Column Description="" MaxRange="1" MinRange="0" Name="Package_Code_Date" SQLDataType="1" SourceColumn="Package_Code_Date"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_1_Container" SQLDataType="1" SourceColumn="Sample_1_Container"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_2_Container" SQLDataType="1" SourceColumn="Sample_2_Container"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_1_Container_Get" SQLDataType="1" SourceColumn="Sample_1_Container_Get"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_2_Container_Get" SQLDataType="1" SourceColumn="Sample_2_Container_Get"/><Column Description="" MaxRange="1" MinRange="0" Name="Machine_Scanned" SQLDataType="1" SourceColumn="Machine_Scanned"/><Column Description="" MaxRange="1" MinRange="0" Name="Machine_Shift_Flag" SQLDataType="4" SourceColumn="Machine_Shift_Flag"/><Column Description="" MaxRange="1" MinRange="0" Name="Maker_Name" SQLDataType="1" SourceColumn="Maker_Name"/><Column Description="" MaxRange="1" MinRange="0" Name="Packer_Name" SQLDataType="1" SourceColumn="Packer_Name"/><Column Description="" MaxRange="1" MinRange="0" Name="Sample_Size" SQLDataType="4" SourceColumn="Sample_Size"/><Column Description="SAP Product Code of the associated Cigarette Audit if Linked" MaxRange="1" MinRange="0" Name="Associated_Cig_Audit_SAP_Code" SQLDataType="1" SourceColumn="Associated_Cig_Audit_SAP_Code"/><Column Description="SAP Product Code of the associated Pack Audit if Linked" MaxRange="1" MinRange="0" Name="Associated_Pack_Audit_SAP_Code" SQLDataType="1" SourceColumn="Associated_Pack_Audit_SAP_Code"/><Column Description="Sample Number of the associated CIg Audit if Linked" MaxRange="1" MinRange="0" Name="Associated_Cig_Audit_Sample_Number" SQLDataType="1" SourceColumn="Associated_Cig_Audit_Sample_Number"/><Column Description="Sample Number of the associated Pack Audit if Linked" MaxRange="1" MinRange="0" Name="Associated_Pack_Audit_Sample_Number" SQLDataType="1" SourceColumn="Associated_Pack_Audit_Sample_Number"/><Column Description="" MaxRange="1" MinRange="0" Name="Machine_SAP_Code" SQLDataType="1" SourceColumn="Machine_SAP_Code"/><Column Description="" MaxRange="1" MinRange="0" Name="Machine_SAP_Desc" SQLDataType="1" SourceColumn="Machine_SAP_Desc"/><Column Description="" MaxRange="1" MinRange="0" Name="Maker_SAP_Code" SQLDataType="1" SourceColumn="Maker_SAP_Code"/><Column Description="" MaxRange="1" MinRange="0" Name="Maker_SAP_Desc" SQLDataType="1" SourceColumn="Maker_SAP_Desc"/><Column Description="" MaxRange="1" MinRange="0" Name="Packer_SAP_Code" SQLDataType="1" SourceColumn="Packer_SAP_Code"/><Column Description="" MaxRange="1" MinRange="0" Name="Packer_SAP_Desc" SQLDataType="1" SourceColumn="Packer_SAP_Desc"/><Column Description="" MaxRange="1" MinRange="0" Name="Action" SQLDataType="1" SourceColumn="Action"/></Columns><Row><Sample_Number>46</Sample_Number><Examination>MKNG_PQC_PACK</Examination><Examination_Title>PQC Pack Audit</Examination_Title><Examination_Desc>Making &amp; Packing PQC Pack Audit Sample Template</Examination_Desc><User_ID>clmf90</User_ID><User_Name></User_Name><Login_By>SYSTEM</Login_By><Test_Number>63</Test_Number><Sample_Time>2007-12-12T13:46:52</Sample_Time><Sample_Status>Complete</Sample_Status><Authorize>No</Authorize><Total_Defects>1</Total_Defects><Carton_Code_Date>-</Carton_Code_Date><Package_Code_Date>7T28D205 11:30</Package_Code_Date><Sample_1_Container>01-01</Sample_1_Container><Sample_2_Container>-</Sample_2_Container><Sample_1_Container_Get>01-01</Sample_1_Container_Get><Sample_2_Container_Get>-</Sample_2_Container_Get><Machine_Scanned>U-MAKER-205</Machine_Scanned><Maker_Name>0205</Maker_Name><Machine_Shift_Flag>1</Machine_Shift_Flag><Packer_Name>0205</Packer_Name><Sample_Size>2</Sample_Size><Associated_Cig_Audit_SAP_Code>2001155</Associated_Cig_Audit_SAP_Code><Associated_Pack_Audit_SAP_Code>-</Associated_Pack_Audit_SAP_Code><Associated_Cig_Audit_Sample_Number>MKNG-PQC-CIG-20071128-0004</Associated_Cig_Audit_Sample_Number><Associated_Pack_Audit_Sample_Number>---</Associated_Pack_Audit_Sample_Number><Machine_SAP_Code></Machine_SAP_Code><Machine_SAP_Desc>MAVERICK LT MENT 100</Machine_SAP_Desc><Maker_SAP_Code>2001155</Maker_SAP_Code><Maker_SAP_Desc>MAVERICK LT MENT 100</Maker_SAP_Desc><Packer_SAP_Desc>MAVERICK LT MENT 100</Packer_SAP_Desc><Packer_SAP_Code></Packer_SAP_Code></Row></Rowset></Rowsets>
    What am I missing?

    Sparks,
    Any reason you are not using the VerticalGrid Applet?
    Did you specify any of the parameters required by the XSL, such as ColumnID and ValueID?
    The XSL appears to only translate a single row node to a column....
    Try using this XSL:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="xalan java">
         <xsl:output encoding="UTF-8" method="xml" media-type="text/xml"/>
         <xsl:template match="/">
              <Rowsets DateCreated="{Rowsets/@DateCreated}" Version="{Rowsets/@Version}" StartDate="{Rowsets/@StartDate}" EndDate="{Rowsets/@EndDate}">
                   <xsl:for-each select="Rowsets">
                        <xsl:copy-of select="FatalError"/>
                        <xsl:copy-of select="Messages"/>
                        <xsl:copy-of select="HyperLinks"/>
                        <xsl:if test="count(/Rowsets/FatalError) = '0'">
                             <Rowset>
                                  <Columns>
                                       <Column Name="Name" SourceColumn="Name" Description="Name" SQLDataType="1" MinRange="0.0" MaxRange="1.0"/>
                                       <Column Name="Value" SourceColumn="Value" Description="Value" SQLDataType="1" MinRange="0.0" MaxRange="1.0"/>
                                  </Columns>
                                  <xsl:for-each select="/Rowsets/Rowset/Row/*[name()]">
                                       <Row>
                                            <xsl:element name="Name">
                                                 <xsl:value-of select="name(.)"/>
                                            </xsl:element>
                                            <xsl:element name="Value">
                                                 <xsl:value-of select="."/>
                                            </xsl:element>
                                       </Row>
                                  </xsl:for-each>
                             </Rowset>
                        </xsl:if>
                   </xsl:for-each>
              </Rowsets>
         </xsl:template>
    </xsl:stylesheet>
    Sam

  • Pivoting rows into columns in Oracle 10g

    Hi,
    I want to pivot rows into column in some optimal way.
    I don't want to go with the DECODE option as the number of columns can be more than 200.
    i have also tried the transpose logic which is making the pl/sql block too huge.
    can i directly query the database for the desired output instead of storing the data into some arrays and displaying rows as columns?

    Hi,
    Here's a dynamic way to do this is Oracle 10, using theSQL*Plus @ command to handle the dynamic parts.
    First, let's see how we would do this using a static query:
    WITH     col_cntr    AS
         SELECT     column_name
         FROM     all_tab_columns
         WHERE     owner          = 'FKULASH'
         AND     table_name     = 'TEST_EMP'
         AND     column_name     NOT IN ('EMP_ID', 'TYPE_VAL')
    ,     unpivoted_data     AS
         SELECT     e.type_val
         ,     c.column_name
         ,     CASE c.column_name
                  WHEN  'X_AMT'  THEN  x_amt     -- *****  Dynamic section 1  *****
                  WHEN  'Y_AMT'  THEN  y_amt     -- *****  Dynamic section 1  *****
                  WHEN  'Z_AMT'  THEN  z_amt     -- *****  Dynamic section 1  *****
              END     AS v
         FROM          test_emp  e
         CROSS JOIN     col_cntr  c
    SELECT       column_name     AS type_val
    ,       SUM (CASE WHEN type_val = 'Q1' THEN v ELSE 0 END)     AS q1     -- ***** Dynamic section 2  *****
    ,       SUM (CASE WHEN type_val = 'Q2' THEN v ELSE 0 END)     AS q2     -- ***** Dynamic section 2  *****
    ,       SUM (CASE WHEN type_val = 'Q3' THEN v ELSE 0 END)     AS q3     -- ***** Dynamic section 2  *****
    ,       SUM (CASE WHEN type_val = 'Q4' THEN v ELSE 0 END)     AS q4     -- ***** Dynamic section 2  *****
    FROM       unpivoted_data
    GROUP BY  column_name
    ORDER BY  column_name
    ;Column names are hard-coded in two places:
    (1) in the sub-query unpivoted_data, we had to know that there were 3 columns to be unpivoted, and that they were called x_amt, y_amt and z_amt. You want to derive all of that from all_tab_columns.
    (2) in the main query, we had to know that there would be 4 pivoted columns in the rsult set, and that they would be called q1, q2, q3 and q4. You want to derive all that from the data actually in test_emp.
    Instead of hard-coding those 2 dynamic sections, have Preliminary Queries write them for you, a split second before you run the main query, by running this script:
    --  Before writing sub-scripts, turn off features designed for human readers
    SET     FEEDBACK    OFF
    SET     PAGESIZE    0
    PROMPT *****  Preliminary Query 1  *****
    SPOOL     c:\temp\sub_script_1.sql
    SELECT    '              WHEN  '''
    ||       column_name
    ||       '''  THEN  '
    ||       LOWER (column_name)     AS txt
    FROM       all_tab_columns
    WHERE       owner          = 'FKULASH'
    AND       table_name     = 'TEST_EMP'
    AND       column_name     NOT IN ('EMP_ID', 'TYPE_VAL')
    ORDER BY  column_name
    SPOOL     OFF
    PROMPT     *****  Preliminary Query 2  *****
    SPOOL     c:\temp\sub_script_2.sql
    SELECT DISTINCT  ',       SUM (CASE WHEN type_val = '''
    ||                type_val
    ||           ''' THEN v ELSE 0 END)     AS '
    ||           LOWER (type_val)          AS txt
    FROM           test_emp
    ORDER BY      txt
    SPOOL     OFF
    --  After writing sub-scripts, turn on features designed for human readers
    SET     FEEDBACK    5
    SET     PAGESIZE    50
    -- Main Query:
    WITH     col_cntr    AS
         SELECT     column_name
         FROM     all_tab_columns
         WHERE     owner          = 'FKULASH'
         AND     table_name     = 'TEST_EMP'
         AND     column_name     NOT IN ('EMP_ID', 'TYPE_VAL')
    ,     unpivoted_data     AS
         SELECT     e.type_val
         ,     c.column_name
         ,     CASE c.column_name
                  @c:\temp\sub_script_1
              END     AS v
         FROM          test_emp  e
         CROSS JOIN     col_cntr  c
    SELECT       column_name     AS type_val
    @c:\temp\sub_script_2
    FROM       unpivoted_data
    GROUP BY  column_name
    ORDER BY  column_name
    ;As you can see, the main query looks exactly like the static query, except that the two dynamic sections have been replaced by sub-scripts. These 2 sub-scripts are written by 2 prelimiary queries, right before the main query.
    As others have said, the fact that you're asking this question hints at a poor table design. Perhaps the table should be permanently stored in a form pretty much like unpivoted_data, above. When you need to display it with columns x_amt, y_amt, ..., then pivot it, using GROUP BY type_col. When you need to display it with columns q1, q2, ..., then pivot it using GROUP BY column_name.

  • Map CdC op transform SEQUENCE and ROW oper column properties

    I am using Map_cdc_op transform, for sequence column and row operation column properties, what fields should i choose?
    My source table has the following columns:
    RMID - primary key int
    RMName - varchar
    created_date datetime
    Last_updated datetime
    target table also has above 4 columns plus one rmLogID column with int data type. which is key column in target.
    does map_cdc_op transform also take care of the surrogate key column id in target? which is "rmlogid".
    thank you very much for the helpful info.

    Suneer I am using SQL serverdatabase, this table is dragged to workspace as source:
    RMID - primary key int
    RMName - varchar
    created_date datetime
    Last_updated datetime
    My task is to capture data changes from this table to target table, for that i a using Map_op_CDC transform, now under the properties SEQUENCE and ROW oper column what fields should i pic.I never used this transform before.
    RMid is the primary key in source, rest teh of teh fields are changeable. i track created_dt and last_updated dates for that row.
    Thanks a lot for the helpful info.

  • Dynamic rows to columns oracle 10g

    Hi,
    I'm using oracle 10g. I have a requiriement where I need to show rows to columns. I have read through many posts but I couldn't find what I wanted to solve. Lots of them are using decode, since they know the values in one column.
    My table has name, value and date. I need to get values for each name at certain date (each distinct date needs to become column). I don't know the name beforehand.
    Name, Value, Date
    T1 100 06-27-2011 09:00:00
    T2 100 06-27-2011 09:00:00
    T1 200 06-27-2011 09:15:00
    T2 150 06-27-2011 09:15:00
    I would also need to use a date range. Someone can request date between 09:00:00 and 09:05:00 or 09:00:00 and 09:30:00 etc.
    I need to print
    Name, 06-27-2011 09:00:00, 06-27-2011 09:15:00
    T1 100 200
    T2 100 150
    Appreciate any advice on this.
    Edited by: user8801143 on 28-Jun-2011 08:23

    With 10g
    see http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:766825833740#2989343200346664698
    search for post on February 11, 2011 and reply on February 14, 2011
    with 11g see the pivot clause

  • Oracle 10g Rows to column

    Hi
    I have folowing scenario. I am getting the result with some query like below :
    STATE      DAY     AVG
    NY        02/02     5
    NY        02/03     10
    NY        02/04     20
    NY        02/05     15
    IL        02/02     23
    IL        02/03     34
    IL        02/04     29
    IL        02/05     9
    FL        02/02     15
    FL        02/03     8
    FL        02/04     9
    FL        02/05     10
    and so on..Now I want to convert it from rows to column, but still want to keep STATE column group by :
    so the result would be :
    STATE   02/02   02/03   02/04   02/05
    NY        5      10       20    15
    IL        23     34       29    9
    FL        15     8        9     10
    and so on...I tried using pivot function, but it didnt work. is it even possible? or I will have to do it in code (.net c#) side.
    Thanks

      1  WITH t AS (
      2  SELECT 'NY' state,'02/02' mydt,5 myavg FROM dual
      3  UNION all
      4  SELECT 'NY','02/03',10 FROM dual
      5  UNION all
      6  SELECT 'NY','02/04',20 FROM dual
      7  UNION all
      8  SELECT 'NY','02/05',15 FROM dual
      9  UNION all
    10  SELECT 'IL','02/02',23 FROM dual
    11  UNION all
    12  SELECT 'IL','02/03',34 FROM dual
    13  UNION all
    14  SELECT  'IL','02/04',29 FROM dual
    15  UNION all
    16  SELECT  'IL','02/05',9 FROM dual
    17  UNION all
    18  SELECT  'FL','02/02',15 FROM dual
    19  UNION all
    20  SELECT  'FL','02/03',8 FROM dual
    21  UNION all
    22  SELECT  'FL','02/04',9 FROM dual
    23  UNION all
    24  SELECT  'FL','02/05',10 FROM dual
    25  )
    26  SELECT * FROM t
    27  PIVOT
    28  (
    29  SUM(myavg)
    30  FOR mydt IN('02/02','02/03','02/04','02/05')
    31  )
    32* ORDER BY state DESC
    SQL> /
    ST    '02/02'    '02/03'    '02/04'    '02/05'
    NY          5         10         20         15
    IL         23         34         29          9
    FL         15          8          9         10Am I missing some thing here ?

  • Transformation of Rows to Column in HANA

    Hi All,
    I have a requirement of converting rows to columns.
    For Ex -
    I have date in my view as below -
    My requirement is I want to convert the above in columnar structure as -
    Please suggest how can I achieve the same.
    Regards,
    Nakul Kothari
    +9987039379

    Hi Nakul,
    There have the points you need to reach your goal, see what would be in SQL:
    select
      status_start_date,
      sum(s1) as s_Initiated,
      sum(s2) as s_cleared,
      sum(s3) as s_scf_associated,
      sum(s4) as s_scf_supervisor
    from (
    select status_start_date,count(*) as s1,0 as s2,0 as s3,0 as s4 from _sys_bic."Spend/AT_GES_CLAIMS_FLAG_DTLS"
    where status_start_date between '2014-01-24' and '2014-01-30' and status='Initiated'
    group by status_start_date
    union all
    select status_start_date,0 as s1,count(*) as s2,0 as s3,0 as s4 from _sys_bic."Spend/AT_GES_CLAIMS_FLAG_DTLS"
    where status_start_date between '2014-01-24' and '2014-01-30' and status='Cleared'
    group by status_start_date
    union all
    select status_start_date,0 as s1,0 as s2,count(*) as s3,0 as s4 from _sys_bic."Spend/AT_GES_CLAIMS_FLAG_DTLS"
    where status_start_date between '2014-01-24' and '2014-01-30' and status='Seek Clarification from Associate'
    group by status_start_date
    union all
    select status_start_date,0 as s1,0 as s2,0 as s3,count(*) as s4 from _sys_bic."Spend/AT_GES_CLAIMS_FLAG_DTLS"
    where status_start_date between '2014-01-24' and '2014-01-30' and status='Seek Clarification from Supervisor'
    group by status_start_date
    group by status_start_date
    Regards, Fernando Da Rós

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

  • From rows to columns

    Hi all, i was wondering if there is a way to transform rows to column using analytical function. i know you can use min or max function and group by but would like to know if the same can be accomplish using analytical function. i am using oracle 9i
    sample data
    WITH table1 AS
       SELECT 'AJD' id , 'BUNIT' code, 1000 myvalue FROM dual UNION all
       SELECT 'AJD' id , 'BCAT' code,  2000 myvalue FROM dual UNION all
       SELECT 'AJD' id , 'BLINE' code, 3000 myvalue FROM dual UNION all
       SELECT 'AJD' id , 'BCEN' code, 4000 myvalue FROM dual UNION ALL
       SELECT 'AAA' id , 'BUNIT' code, 5000 myvalue FROM dual UNION all
       SELECT 'AAA' id , 'BCAT' code,  6000 myvalue FROM dual UNION all
       SELECT 'AAA' id , 'BLINE' code, 7000 myvalue FROM dual UNION all
       SELECT 'AAA' id , 'BCEN' code, 8000 myvalue FROM dual
    desire output
    ID     UNIT    CAT    LINE    CEN
    ADJ    1000    2000   3000    4000
    AAA    5000    6000   7000    8000
      if this can be done using analytical function, please provide query if possible. thanks

    Thanks for the sample data.
    I'm not sure why you want to do this with analytic fiunctions instead of aggregates, it seems like a lot of effort for no gain, but this works on 9.2.0.8.0, and should work on other versions of 9i.
    SQL> WITH table1 AS
      2  (
      3     SELECT 'AJD' id , 'BUNIT' code, 1000 myvalue FROM dual UNION all
      4     SELECT 'AJD' id , 'BCAT' code,  2000 myvalue FROM dual UNION all
      5     SELECT 'AJD' id , 'BLINE' code, 3000 myvalue FROM dual UNION all
      6     SELECT 'AJD' id , 'BCEN' code, 4000 myvalue FROM dual UNION ALL
      7     SELECT 'AAA' id , 'BUNIT' code, 5000 myvalue FROM dual UNION all
      8     SELECT 'AAA' id , 'BCAT' code,  6000 myvalue FROM dual UNION all
      9     SELECT 'AAA' id , 'BLINE' code, 7000 myvalue FROM dual UNION all
    10     SELECT 'AAA' id , 'BCEN' code, 8000 myvalue FROM dual)
    11  SELECT DISTINCT id, MAX(DECODE(code, 'BUNIT', myvalue)) OVER(PARTITION BY id) unit,
    12         MAX(DECODE(code, 'BCAT', myvalue)) OVER(PARTITION BY id) cat,
    13         MAX(DECODE(code, 'BLINE', myvalue)) OVER(PARTITION BY id) line,
    14         MAX(DECODE(code, 'BCEN', myvalue)) OVER(PARTITION BY id) cen
    15  FROM table1;
    ID        UNIT        CAT       LINE        CEN
    AJD       1000       2000       3000       4000
    AAA       5000       6000       7000       8000John

Maybe you are looking for