Table PIVOT (10.2.0.4.0) problem

Hi all!

Given the following data set:
CREATE TABLE unpivoted
(
   id    NUMBER,
   typ   VARCHAR2 (30),
   val   VARCHAR2 (30)
);

INSERT INTO unpivoted VALUES (1, 'A', 'A123456');
INSERT INTO unpivoted VALUES (1, 'B', 'B455245');
INSERT INTO unpivoted VALUES (2, 'A', 'A835666');
INSERT INTO unpivoted VALUES (2, 'B', 'B126832');
INSERT INTO unpivoted VALUES (2, 'C', 'C123456');
INSERT INTO unpivoted VALUES (3, 'C', 'C545662');
INSERT INTO unpivoted VALUES (3, 'D', 'D525254');
INSERT INTO unpivoted VALUES (3, 'E', 'E636656');
Using only SQL and avoiding scalar subqueries, how can I get the data to display as well?
ID    A        B        C        D        E
===== ======== ======== ======== ======== =======
1     A123456  B455245  <NULL>   <NULL>   <NULL>
2     A835666  B126832  C123456  <NULL>   <NULL>     
3     <NULL>   <NULL>   C545662  D525254  E636656
I understand that it probably involves some kind of operation of pivot, but I'm just not smart enough to apply the examples I've seen elsewhere in my data!

Thank you very much!

Hello

Welcome to the forum!

In any version (Oracle 8.1 or higher) you can do this way:

SELECT       id
,       MIN (CASE WHEN typ = 'A' THEN val END)   AS a
,       MIN (CASE WHEN typ = 'B' THEN val END)   AS b
,       MIN (CASE WHEN typ = 'C' THEN val END)   AS c
,       MIN (CASE WHEN typ = 'D' THEN val END)   AS d
,       MIN (CASE WHEN typ = 'E' THEN val END)   AS e
FROM       unpivoted
GROUP BY  id
ORDER BY  id
;

Many examples you saw maybe used to COUNT or SUM instead of MIN. You can use a function of aggregation with this technique.

For more info on pivots, see the FAQ forum {message identifier: = 9360005}

Thanks for posting the CREATE TABLE and INSERT statements; It's really useful!

Tags: Database

Similar Questions

  • Remove the view grid lines table Pivot

    Hi people,

    How to remove the view grid lines table Pivot? Any script must be integrated? If Yes, then please give me the details.

    Kind regards

    AJ

    This is how you do it,

    Take each of your lines and measures in the PivotTable, and then select the "Values of Format" option when you click on the menu options for each of the lines and the measures.

    In that set the border color to your "background color" which could be "white" and make sure that the border post has the value 'None '.

    That's your vision of pivot will not have the grid lines more...

    Please indicate if this help

  • OBIEE 11g - lock dimensions tiles so that users cannot modify a table / pivot

    Hi, in OBIEE 11 g, is it possible to "lock" the tiles so that users cannot re - arrange the dimensions of a table / pivot / graph? In general, we would like to have this feature, users, but for a couple of key reports, we might want to "lock it" so end users can not change.

    Thanks in advance!
    Scott

    Hi Scott,.

    Answer is no. It's still a bug with no workaround.

    Rgds,
    DpKa

  • RTF generated using option table Pivot BI publisher but do not generate output when executed in ebs 12.1.3 simultaneous

    Hello

    I built a rdf using pivot option BI-Publisher. When I saw on my local machine it generates the output correctly. But what rtf downloaded in EBS and simultaneous is run it see as finished phase and status as a warning. Output is generated in xml format, where, as I've specified it by default as excellent output.

    Kindly help to solve the problem.

    I found the solution for it. Now, I can generate the table outside by the EBS rtf pivot.

  • Sorting the rows in the table based on the first character - speed-problem

    Hello

    I have a text file with a lot of lines. The file is imported into LV, and must be sorted by the first character.

    It works very well, but gets very slow lines if too.

    If 40,000 lines then take 0. 2 s.

    If 400,000 lines then the time rises with factor 100 in 25 years.

    If 1,000,000 lines then the weather is dry 222 (~ 4 min)

    Attached is the code which sorts a table to text.

    The number of lines can be set with the meter of the loop (Loop-counter * 4 = NumberOfLines)

    The elapsed time is displayed.

    How to speed this up? (Or check the more linear time number of rows)

    Thank you

    You can try something like that. If you treat a table line by line, you end up taking a lot of time.

    Sorry I have a problem of fixing of the VI.

  • Table 1 d in the while loop problem

    Hello world

    I searched this problem in the forum, but seems only some positions quite didn't answer.

    I'm stuck by saving the data in a while loop.

    I have a while loop, within which there are two exits in each iteration, I want to add to a table, and then after the while loop is completed, I want to combine these two tables in a 2D array (since the release of them must be a pair in my case).

    I tried 'Build array' and 'transpose array' and 'write in spreadsheet' element set, which maintains concerns me is how simply to add the result in a 1 d table during each iteration, these 2 outputs are in digital format, which prevent to directly create a table for it.

    Any idea is appreciated,

    Thank you

    Chen Kunsheng

    Here are some other ways to insert.

    It may be useful

  • Changing table via the package with immediate execution (problem)

    Hello

    I have a unusual problem. I have a package that contains the procedure that via execute immediate statement, creates a table on which he performs different actions for example:

    -alter table some_owner.dummy_table noparallel

    -create index some_owner.idx_name on some_owner.dummy_table (column)...

    But I only managed to run move and create synonym table/public via execute immediate statement. Actions as alter table and create index fails with error ORA-01031: insufficient privileges.

    Note If call these commands outside the package (a simple script) this all done OK.

    I found a way, where I set the AUTHID CURRENT_USER command create package statement. In this way all the actions executed OK.

    I wonder why I can't change the table via the package with immediate execution on a table that is in the tablespace "SOME_TABLESPACE" and the title of owner "SOME_OWNER", without putting the AUTHID command on the package.

    There must be a problem why a package cannot change the table which is owned by the user 'SOME_OWNER '.

    All useful responses would be appreciated.

    I have a unusual problem.

    No - you don't have. This question has been answered SEVERAL times on this forum and others.

    But I only managed to run move and create synonym table/public via execute immediate statement. Actions as alter table and create index fails with error ORA-01031: insufficient privileges.

    OK - your username doesn't have privileges to do these operations or only received privileges through roles.

    Roles are DISABLED in named PL/SQL blocks that use of the AUTHOR's rights.

    Note If call these commands outside the package (a simple script) this all done OK.

    I found a way, where I set the AUTHID CURRENT_USER command create package statement. In this way all the actions executed OK.

    Of course - the roles are NOT disabled in named PL/SQL blocks that use the rights of the APPELLANT or in anonymous blocks.

    I wonder why I can't change the table via the package with immediate execution on a table that is in the tablespace "SOME_TABLESPACE" and the title of owner "SOME_OWNER", without putting the AUTHID command on the package.

    Well now you know!

  • Help with several table Pivot

    Oracle 11.2.0.1

    Windows

    create table tab1

    (

    Identification number,

    number of Java,

    number of DotNet,

    The Oracle number,

    number of MSSQL,

    number of php

    )

    /

    insert into tab1 values (1,10,20, null, 30, null);

    insert into tab1 values (2, null, 20, 30, null, null);

    insert into tab1 values (3,10,20,30,40,50);

    insert into tab1 values (4, null, null, null, null, null);

    create table tab2

    (

    Identification number,

    number of Java,

    number of DotNet,

    The Oracle number,

    number of MSSQL,

    number of php

    )

    /

    insert into values tab2 (1,10,20, null, 30, 40);

    Enter tab2 values (2, null, 20, 30, null, null);

    insert into values tab2 (3,10,20, null, 40, null);

    Enter tab2 values (4, null, 20, null, null, null);

    SQL > select * from tab1;

    ID DOTNET ORACLE MSSQL PHP JAVA

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

    1         10         20                    30

    2                    20                    30

    3         10         20         30         40         50

    4

    SQL > select * from tab2.

    ID DOTNET ORACLE MSSQL PHP JAVA

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

    1         10         20                    30         40

    2                    20                    30

    3         10         20                    40

    4                    20

    SQL >

    Power required:

    ID DOTNET ORACLE MSSQL PHP JAVA

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

    1-10-20 - 30 - <-table Tab1

    10-20 - 30 40 <-table Tab2

    < < < < A LINE VIRGIN > > >

    2        ---         20        ---         30        ---

    ---         20        ---         30        ---

    < < < < A LINE VIRGIN > > >

    3         10         20         30         40         50

    10         20        ---         40        ---

    < < < < A LINE VIRGIN > > >

    4        ---        ---        ---        ---        ---

    ---         20        ---        ---        ---

    Thank you.

    You need not pivot for this. The pivot is something completely different. Use weighted UNION ALL:

    with t1 as)

    Select tab1.*,

    1 weight

    of tab1

    Union of all the

    Select tab2.*,

    2 weight

    of the tab2

    ),

    T2 as)

    Select *.

    from t1

    Union of all the

    Select distinct id,

    Java NULL,

    dotnet null,

    Oracle NULL,

    MSSQL null,

    PHP null,

    3 weight

    from t1

    )

    Select the weight of case

    When 1 then id

    identifier of the end.

    Java,

    DotNet,

    Oracle,

    MSSQL,

    PHP

    the t2

    order by t2.id

    T2. Weight

    /

    ID DOTNET ORACLE MSSQL PHP JAVA
    ---------- ---------- ---------- ---------- ---------- ----------
    1         10         20                    30
    10         20                    30         40

    2                    20                    30
    20                    30

    3         10         20         30         40         50
    10         20                    40

    4
    20

    ID DOTNET ORACLE MSSQL PHP JAVA
    ---------- ---------- ---------- ---------- ---------- ----------

    12 selected lines.

    Scott@pdborcl12 >

    SY.

  • Display table pivot OBIEE dashboard error

    Hello
    I created a dashboard that has the PivotTable. But I don't want to Pivot table to display in the dashboard with the name of the PivotTable. I checked the option to display show where we can show pivot table or title. When I select PivotTable, see the. It only displays the PivotTable. It does not display the name of the PivotTable. Someone help me on this please.

    user12058206 wrote:
    Hello
    I created a dashboard that has the PivotTable. But I don't want to Pivot table to display in the dashboard with the name of the PivotTable. I checked the option to display show where we can show pivot table or title. When I select PivotTable, see the. It only displays the PivotTable. It does not display the name of the PivotTable. Someone help me on this please.

    What you need to do is to go in your analysis (report) and the Compound Layout, display only the title and Pivot Table. The compound in default mode with the view on the Table. Click on the Red to get rid of this and add 'x' view > table crossed to get the pivot table view.

    Then in your dashboard, have the section pointing compound mode (default setting).

  • How to change the default color for tables/pivot tables

    Can someone share how to change the default font color or background for tables and PivotTables? In my view, there is a CSS or XML setting I need to change.

    It is difficult to read the text when negative numbers are RED and the overall totals have a default NAVY background fill. I'm trying to find out how to change the color of these without doing it manually to each report.

    Thank you.

    Jin

    For pivot Table

    Go to OracleBI\oc4j_bi\j2ee\home\applications\analytics\analytics\res\s_oracle10\b_mozilla_4\views.css

    Change the PivotTable section code

    For table veiw

    Go to OracleBI\oc4j_bi\j2ee\home\applications\analytics\analytics\res\s_oracle10\b_mozilla_4\views.css

    Change the section of the Table

    PS:take backup your original file before you edit any css file. Don't forget to give the points/close the thread

    Thank you
    saichand.v

  • Expansion of a width of table/pivot

    Whenever I have too many columns in a table or pivot in the responses, some of the format of the column width gets reduced, for example:

    01-
    Mar
    -09

    While I want the column to appear as follows: 1st March 09. Even when I try to increase the size of the value of the column to 60 for example, it does nothing. Whereas if it is a report that has some columns and I increase the size, does so successfully, but if it is a report with the number of columns, that it does not.

    Any clue?

    Try to play around with the CSS nowrap command.

    Concerning

    John
    http://obiee101.blogspot.com/

  • Queries on a huge table pivot

    Hello

    I have the following table t1 with 200 million records. I wrote pivot as queries below

    create table t1 (c1 number, c2 varchar2 (2), c3 varchar2 (2));

    insert into t1 values (10, '01', 'MD');
    insert into t1 values (10, '02', "IA");
    insert into t1 values (20, '02', "IA");
    insert into t1 values (20, '03', 'AZ');
    insert into t1 values (30, '03', 'MD');
    commit;

    Select
    Count (case when c1 = 10 then c1 otherwise null) end v_c1_10.
    Count (case when c1 = 20 then c1 otherwise null) end v_c1_20.
    Count (case when c1 = 30 then c1 otherwise null) end v_c1_30.
    Count (case when c2 = '01' then c2 otherwise null) end v_c2_01.
    Count (case when c2 = '02' then c2 otherwise null) end v_c2_02.
    Count (case when c2 = '03' then c2 otherwise null) end v_c2_03.
    Count (case when c3 = 'MD' then end c3 otherwise null) v_c3_MD,.
    Count (case when c3 = "IA" then end c3 otherwise null) v_c3_IA,.
    Count (case when c3 = 'AZ' then end c3 otherwise null) v_c3_AZ
    - - -
    - - -
    - - -
    Of
    (
    Select * from t1
    )


    I need ot get the count on 110 columns to a dataset of 200 million records.

    Can the above query can be rewritten in an effective way?

    Thank you

    Moreover, you don't need this:

    Of
    (
    Select * from t1
    )

    Simply use: from t1

  • Taking a class SQL and make a table for a project and have a problem with my table does not.

    Here is my syntax

    create the table student_data

    (Stu_ID, varchar2 (4))

    Lname varchar2 (12),

    Fname varchar2 (12),

    address varchar2 (25).

    zip of varchar2 (10),

    Phone # varchar2 (15).

    E-mail varchar2 (35),

    AA_degree_ID varchar2 (2),

    AA_grad_year varchar2 (4).

    Status (10),

    BA_start_date varchar2 (4).

    Major_ID varchar2 (5).

    Advisor_ID varchar2 (4).

    Student degree Plan developed varchar2 (4).

    Graduation of Student application complete varchar2 (4).

    Student program assessment varchar2 (4));

    and says error starts on line 1

    Error report-

    SQL error: ORA-00907: lack the right parenthesis

    00907 00000 - "lack the right parenthesis.

    If it makes a difference that I wrote all the outside syntax in Notepad ++ and copy pasted above.

    Student degree Plan developed varchar2 (4).

    Graduation of Student application complete varchar2 (4).

    Student program assessment varchar2 (4));

    If someone else sees this i figurde my answer these he i connected with underscores and I took thing the table was trying ot and then told me they had long in length, so I had to shorten.

  • Table of contents - several line Page # alignment problem

    Hi all

    I have a Table of contents, in which I have a few entries that develops in two lines.

    When the contained entries Table expand to two lines, leader are not added and page numbers shows next to the entrance.

    See Image below: entrance with RED ARROW - 1.9.4 indicates Page 44 without a leader:

    No explanation will be appreciated.

    Thank you.

    No need to change the titre3 paragraphs. Look at the reference to the table of contents page. There is probably a page containing the TOC tag that defines the format of the entries in the table of contents. There the paragraphs which badges are derived from paragraphs in the main document that generate entries by adding OCD to the original label. Thus, registrations for titre3 paragraphs are marked Heading3TOC. The reference page has a Heading3TOC which looks like point something lke:

    <$paranumonly>  <$paratext>.........................<$pagenum>

    When there is a tab between <$paranumonly>character and <$paratext>that moves from the left side of the <$paratext>2.0 cm, and another tab between <$paratext>and <$pagenum>that moves to the right of the <$pagenum>-12.0 cm.

    I mean put a space before the second tab. You can also add an additional tab before the <$pagenum>.

    -Lynne

  • Table PIVOT query need help

    I have a requirement as below-

    The output I want as below.
    EMP_ID    LAPTOP   DESK LAPTOP    LCD MONITOR     ROUTER
    ------------------------------------------------------------------------------------------
    100         Y            Y                      Y                      Y
    101         Y            N                      Y                      N
    102         N            Y                      N                      N
    -----------------------------------------------------------------------------------------  
    I wrote a query as below with hard-code the code produced.
    SELECT EMP_ID, 
               NVL(MAX(DECODE(PROD_ID,10,'Y',NULL)),'N') LAPTOP,
               NVL(MAX(DECODE(PROD_ID,11,'Y',NULL)),'N') DESK_LAPTOP,
               NVL(MAX(DECODE(PROD_ID,12,'Y',NULL)),'N') LCD_MONITOR,
               NVL(MAX(DECODE(PROD_ID,13,'Y',NULL)),'N') ROUTER
      FROM XX_EMP a
    GROUP BY EMP_ID;
    I want a solution without Hardcoding code product, because if we need to introduce a new product in the product table, then the output will be fit dynamically...

    Is it possible to write the query like this?

    Is my version of DB - Oracle 10 g

    Table creation script-
    CREATE TABLE XX_EMP (
                         EMP_ID     NUMBER,
                         PROD_ID    NUMBER,
                         SALE_DT    DATE
                         );
                         
    INSERT INTO XX_EMP VALUES(100,10,sysdate-15);
    INSERT INTO XX_EMP VALUES(100,11,sysdate-14);
    INSERT INTO XX_EMP VALUES(100,12,sysdate-14);
    INSERT INTO XX_EMP VALUES(100,13,sysdate-13);
    INSERT INTO XX_EMP VALUES(101,11,sysdate-11);
    INSERT INTO XX_EMP VALUES(101,13,sysdate-10);
    INSERT INTO XX_EMP VALUES(102,12,sysdate-10);
    COMMIT;
    
    CREATE TABLE XX_PRODUCT(PROD_ID     NUMBER,
                            PROD_DES    VARCHAR2(50)
                            );
                            
    INSERT INTO XX_PRODUCT VALUES(10,'LAPTOP');
    INSERT INTO XX_PRODUCT VALUES(11,'DESK LAPTOP');
    INSERT INTO XX_PRODUCT VALUES(12,'LCD MONITOR');
    INSERT INTO XX_PRODUCT VALUES(13,'ROUTER'); 
    COMMIT;

    Hello

    See this thread for more options:
    Re: County report and the sum of the number of rows by multiple columns

    It seems that the aggregation of the chain might be the best for this work.

    For example:
    I guess you don't want to view the whole xx_emp table in all reports. In the first auxiliary request, emp_summary, below I've limited the output lines with the sale_dt in the last 30 days. You can change this to anything else, or omit the WHERE all clause if you do not want to include the entire table.
    I assume that you will not necessarily interested in the whole xx_product. In the second auxiliary request, product_summary, I've limited the output to products that appear effectively in emp_summary. Again, you can use any conditions you want, or include all the lines.
    The main request is the UNION of 3 games: 2 produce header lines, and the third produces the bulk of the production. Note that (as far as Oracle is concerned) there are only 2 columns in the output. The last of them will be foramatted to resemble a variable number of columns.

    VARIABLE     column_width     NUMBER
    EXEC  :column_width := 12;
    
    SET   PAGESIZE         0
    
    WITH     emp_summary     AS
    (
         SELECT    emp_id, prod_id
         FROM       xx_emp
         WHERE       sale_dt     >= TRUNC (SYSDATE - 30)     -- or whatever
         GROUP BY  emp_id, prod_id
    )
    ,     product_summary     AS
    (
         SELECT     prod_id
         ,     SUBSTR (prod_des, 1, :column_width)     AS short_prod_des
         ,     ROW_NUMBER () OVER (ORDER BY prod_id)     AS r_num
         FROM     xx_product
         WHERE     prod_id     IN (
                          SELECT  prod_id
                          FROM    emp_summary
                      )
    )
           --
           --     =====  First Header Line: Product Names  =====
           --
    SELECT       NULL          AS emp_id
    ,       REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( short_prod_des
                                          , :column_width
                                    )
                             , '~'
                             )
                , '~'
                , ' '
                )     AS txt
    FROM       product_summary
    WHERE       CONNECT_BY_ISLEAF     = 1
    START WITH     r_num          = 1
    CONNECT BY     r_num          = PRIOR r_num + 1
           --
    UNION ALL --     =====  Second Header Line: Hyphens  =====
           --
    SELECT       NULL          AS emp_id
    ,       SYS_CONNECT_BY_PATH ( LPAD ( '-'
                                     , :column_width
                             , '-'
                             )
                         , ' '
                         )          AS txt
    FROM       product_summary
    WHERE       CONNECT_BY_ISLEAF     = 1
    START WITH     r_num          = 1
    CONNECT BY     r_num          = PRIOR r_num + 1
           --
    UNION ALL --     =====  Body of Report  =====
           --
    SELECT       e.emp_id
    ,       REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( NVL2 ( e.prod_id
                                                     , 'Y'
                                        , 'N'
                                        )
                                    , :column_width
                                    )
                             , '~'
                             )
                , '~'
                , ' '
                )   AS txt
    FROM          product_summary     p
    LEFT OUTER JOIN     emp_summary     e  PARTITION BY (e.emp_id)
                                   ON  p.prod_id     = e.prod_id
    WHERE       CONNECT_BY_ISLEAF  = 1
    START WITH     p.r_num          = 1
    CONNECT BY     p.r_num          = PRIOR p.r_num + 1
         AND     e.emp_id     = PRIOR e.emp_id
           --
           --  =====  Common ORDER BY Clause  =====
           --
    ORDER BY  emp_id     NULLS FIRST
    ,            txt
    ;
    

    Output:

    `             LAPTOP  DESK LAPTOP  LCD MONITOR       ROUTER
            ------------ ------------ ------------ ------------
       100             Y            Y            Y            Y
       101             N            Y            N            Y
       102             N            N            Y            N
    

    There are a few things you might want to change.
    For example, if the first product_name is: characters column_width, the ORDER BY clause will put hyphens, first. Which can be fixed by putting the UNION in a subquery, so that you can ORDER BY columns that do not appear in the output.

    You could also include all product_des, dividing each item into pieces of no. more: column_width characters. I'll leave that as an exercise.

    Published by: Frank Kulash, April 12, 2011 17:49
    Example of grouping of extra chain

Maybe you are looking for

  • Pavilion 15-N240ee: Wifi driver problem

    Hello When I close the lib (plugged), wifi turned off and my download stops all power saving settings is disable and lib is the 'status quo' (connected) I think that this problem is a bug in the driver Please help me

  • Qosmio X 305 crashes when you play

    I have a problem, whenever I play a game, my laptop goes down, it only lasts 10 minutes of game (like COD 4, GOW, Crysis) and need me a black screen where I need to do a hard reboot. I tried, change drivers, control my temperature, even a reinstall o

  • Why are my text Messages on my MAC "delivered no message?"

    Why my instant text messages are going back as NOT DELIVERED sometimes and not others.  I want that all my texts and instant messages to appear on my MAC (Yosemite) messages, my iPhone and my iPad.

  • Stand By worked in XP Home, but not XP Pro SP3 on the same machine

    Stand By worked in XP Home, but not XP Pro SP3 on the same machine.How can I get Stand By under XP Pro SP3? * title Moose - see http://stlcc.org/SYS/Standby.bmp for the issue.*

  • Advice on how to proceed after installing the OS on Hp Pavilion 2010ax g6

    Hello I recently bought a HP Pavilion laptop 2010ax from flipkart.com g6. I installed win 7 ultimate laptop and was stuck there. I didn't know how to proceed further as ethernet, wireless, bluetooth, webcam, touchpad driver, graphics etc. If anyone c