UNPIVOT with two set of records

Hi Gurus,
I am using Oracle 11g Release 11.2.0.1.0
OS: windows
Table Structure
EMPL0YEE_ROLE
employee_id number
ROLE_1 VARCHAR2(10)
ROLE_2 VARCHAR2(10)
ROLE_3 VARCHAR2(10)
ROLE_4 VARCHAR2(10)
ROLE_5 VARCHAR2(10)
MANAGER_1 VARCHAR2(10)
MANAGER_2 VARCHAR2(10)
MANAGER_3 VARCHAR2(10)
MANAGER_4 VARCHAR2(10)
MANAGER_5 VARCHAR2(10)
DATA
EMPLOYEE_ID MANAGER_1 MANAGER_2 MANAGER_3 MANAGER_4 MANAGER_5 ROLE_1 ROLE_2 ROLE_3 ROLE_4 ROLE_5
1345 John Mike Ram Kumar DBA Sql Dev PLSQL Admin
1 rows selected
I want a output like this
EMPLOYEE_ID MANAGER ROLE1
1345 John DBA
1345 Mike SQL DEV
1345 Ram PLSQL
1345 Kumar Admin
I have tried with UNPIVOT
I am able to get manager and role in two different queries
SELECT
EMPLOYEE_ID
,MANAGER
FROM
EMPLOYEE_ROLE
UNPIVOT( MANAGER
FOR col1
in (MANAGER_1,
MANAGER_2,
MANAGER_3,
MANAGER_4,
MANAGER_5
EMPLOYEE_ID            MANAGER   
1345                   John      
1345                   Mike      
1345                   Ram       
1345                   Kumar     
4 rows selected
SELECT
EMPLOYEE_ID
,ROLE1
FROM
EMPLOYEE_ROLE
UNPIVOT( ROLE1
FOR col1
in (ROLE_1,
ROLE_2,
ROLE_3,
ROLE_4,
ROLE_5
EMPLOYEE_ID            ROLE1     
1345                   DBA       
1345                   Sql Dev   
1345                   PLSQL     
1345                   Admin  
4 rows selected
WHEN I tried adding 2 UNPIVOT clauses i got
SELECT
EMPLOYEE_ID
,ROLE1
,MANAGER
FROM
EMPLOYEE_ROLE
UNPIVOT( ROLE1
FOR col1
in (ROLE_1,
ROLE_2,
ROLE_3,
ROLE_4,
ROLE_5
UNPIVOT( MANAGER
FOR col2
in (MANAGER_1,
MANAGER_2,
MANAGER_3,
MANAGER_4,
MANAGER_5
it is giving me cartesian product as given below
EMPLOYEE_ID            ROLE1      MANAGER    
1345                   DBA        John      
1345                   DBA        Mike      
1345                   DBA        Ram       
1345                   DBA        Kumar     
1345                   Sql Dev    John      
1345                   Sql Dev    Mike      
1345                   Sql Dev    Ram       
1345                   Sql Dev    Kumar     
1345                   PLSQL      John      
1345                   PLSQL      Mike      
1345                   PLSQL      Ram       
1345                   PLSQL      Kumar     
1345                   Admin      John      
1345                   Admin      Mike      
1345                   Admin      Ram       
1345                   Admin      Kumar     
16 rows selectedIs it possible to add two unpivots in a single UNPIVOT query.
Any help is appreciated.
Thanks!
Regards,
Gatha
Edited by: Gatha on Sep 27, 2011 1:50 PM
Edited by: Gatha on Sep 27, 2011 1:51 PM
Edited by: Gatha on Sep 27, 2011 1:53 PM
Edited by: Gatha on Sep 27, 2011 2:40 PM

You can use an object so that the couple (MANAGER, ROLE) is considered as a single column to unpivot :
SQL> create or replace type role_manager_obj as object (manager varchar2(10), role varchar2(10))
  2  /
Type created
SQL> WITH emp AS (
  2    SELECT employee_id
  3         , case when manager_1 is not null then role_manager_obj(manager_1, role_1) end as rm1
  4         , case when manager_2 is not null then role_manager_obj(manager_2, role_2) end as rm2
  5         , case when manager_3 is not null then role_manager_obj(manager_3, role_3) end as rm3
  6         , case when manager_4 is not null then role_manager_obj(manager_4, role_4) end as rm4
  7         , case when manager_5 is not null then role_manager_obj(manager_5, role_5) end as rm5
  8    FROM employee_role
  9  )
10  SELECT employee_id
11       , treat(rm_obj as role_manager_obj).manager as manager
12       , treat(rm_obj as role_manager_obj).role as role
13  FROM emp
14  UNPIVOT(rm_obj FOR rm IN (rm1, rm2, rm3, rm4, rm5))
15  ;
EMPLOYEE_ID MANAGER    ROLE
       1345 John       DBA
       1345 Mike       SQL DEV
       1345 Ram        PLSQL
       1345 Kumar      Admin
Or, doing it the old way, with a cross join :
SQL> WITH five_rows AS ( SELECT level i FROM dual CONNECT BY level <= 5 )
  2  SELECT employee_id
  3       , manager
  4       , role
  5  FROM (
  6    SELECT e.employee_id
  7         , case t.i when 1 then manager_1
  8                    when 2 then manager_2
  9                    when 3 then manager_3
10                    when 4 then manager_4
11                    when 5 then manager_5
12           end as manager
13         , case t.i when 1 then role_1
14                    when 2 then role_2
15                    when 3 then role_3
16                    when 4 then role_4
17                    when 5 then role_5
18           end as role
19    FROM employee_role e
20         CROSS JOIN five_rows t
21  )
22  WHERE manager IS NOT NULL
23  ;
EMPLOYEE_ID MANAGER                                  ROLE
       1345 John                                     DBA
       1345 Mike                                     SQL DEV
       1345 Ram                                      PLSQL
       1345 Kumar                                    Admin
Edited by: odie_63 on 27 sept. 2011 13:04

Similar Messages

  • When I migrated to iCloud I ended up with two sets of calendars.

    When I migrated to iCloud I ended up with two sets of calendars.  I now have two calendars named "Home", two calendars named "Work", etc.  I can't just delete one set, because my events somehow ended up split across the calendars.
    Is there any way to merge these two sets of calendars into one set?  I can use Export and Import to move future events from one calendar to another, but I also want to move past events.
    FWIW, the duplicate calendars appear on all my devices and also on iCloud.

    I fixed this by doing the following:
    Renamed one set of calendars to "name-1" and the other set of calendars to "name-2".  Gave the different calendars different numbers.
    Located all events in the "-1" calendars by turning off the "-2" calenders.  Used the Year view to make sure I wasn't missing anything.
    Changed each event to point to the "-2" calendar.
    Deleted all the "-1" calendars.
    Removed the "-2" from all the remaining calendars.
    This didn't end up taking that long, and the changes were immediately reflected on all my devices.

  • Numbers Charts:  Stacked bar chart with two sets of data

    Hi....newbie with first post
    I'm trying to create a stacked bar chart with two lots of comparative data.
    So, there is Basic + Overtime for April 2010 as one stack (ie the basic is one colour, stacked on top is the overtime in a different colour)
                      Basic + Overtime for April 2011 next to it.
    I can get April figures next to one another as four separate columns (2 x basic, 2 x overtime), or I can get all four figures in a single stack, but I can't to two columns (one for each year) of basic and overtime.
    Any ideas?

    UK,
    What you are trying to do will require that you interleave the two sets of two-series data into one longer two-set series, and then plot just the one pair of series. Clear as mud?
    What I'm saying is that you have to fake out the program into thinking that you have just two sets, not four.
    You will have one series that has, for instance Apr 2010 Basic, Apr 2011 Basic, May, 2010 Basic, May 2011 Basic, etc.. This other series in that chart will have Apr 2010 Overtime, Apr 2011 Overtime, May 2010 Overtime, May 2011 Overtime, etc.
    The bad thing here (I'll anticipate your first complaint) is that 2010 and 2011 data will have the same color for Basic and another color will be assigned for both 2010 Overtime and 2011 Overtime.
    To get separate color sets for 2010 and 2011 data pairs, you will have to create two charts and overlay them. Both charts would be built in the alternating year format described above, but in one chart all the 2010 data would be blank and in the other chart all the 2011 data would be blank, leaving spaces in the chart for the data from the other chart to nest in.
    Jerry

  • Datatable with two rows per record

    Hi,
    I have a table I need to render in a jsf datatable.
    The problem each record has two rows, just like this:
    <tr class="resultsrow1">
    <td rowspan="2"><strong>Life & Pensions Administrator</strong></td>
    <td>Insurance</td>
    <td>Contract</td>
    <td>Cork</td>
    <td>LPA/2910</td>
    </tr>
    <tr class="resultsrow1">
    <td colspan="4"><u>Job Description:</u> Minimum 3 - 4 years experience required and progression towards qualification beneficial. Dublin 2, 4 and South Co. Dublin. Salary &euro; depends on experience.</td>
    </tr>
    How can I render this with a JSF datatable, or can I at all. I can't see a way anywhere and I can't seem to set the colspan anywhere?
    Thanks in advance,
    Keith

    Yeah. Sadly, colspan and rowspan are missing from dataTable. Maybe the implications were just too hairy.
    I see two possible solutions:
    (1) outer, inner tables. Outer table contains your section name ("something something Administrator"?) in lefthand column, inner table in righthand column. Inner table contains two rows: firstHalfOfData, secondHalfOfData. Used fixed widths in CSS styles to make your columns line up across instances of the inner table.
    (2) one table, rows composed of alternating firstHalf, secondHalf beans. Leftmost column of firstHalf row contains section name. Leftmost column of secondHalf row is blank.
    I'll be interested to know how that turns out. :)
    John.

  • Printing same smartform with different set of records continuously.

    Hi SDNer,
    Suppose a table contain Studet info like the following one:
    ID   Name    Sub1       Sub2     Sub3
    1    xxx        sb11       sb12     sb13
    2    yyy        sb21       sb22     sb23
    3    zzz        sb31       sb32     sb33
    Think that the smartform contain only a single page(Suppose smartform of student result).In the Selection screen User can put the range of ID.When User put 1-3 the smartform print 3 pages---1st for the student with id 1,2nd for student with id 2 and 3rd for student with id 3.when user put the input 1-2 the smartform print 2 pages.I need your help to accomplish such a scenario.
    Thanks in advance.
    Regards,
    Sarbajit.

    Hi,
    As per user's selection criteria you need fetch data in your driver program and need to store it in an internal table. The same internal table you need to pass to smartform. to handle your requirement of displaying one student record per page you need to use COMMAND node (with some condition). Using this you can force your smartform for page-break.
    I hope this will help you to start and explore further.
    Regards,
    Sambaran Ray

  • Web Remote DVR with two set-top boxes

    I'd like to access one or both of the two DVRs I have from the web so I can setup recordings when I am not at home.   When I login to FIOS TV Central it recognizes my two QIP6416 HD DVRs.  However, If I go to TV listings I do not get any option to record.     I think it is because I have two DVR boxes and it does not know how to handle more than one.  
    If at the TV remote control I do a MENU button > Settings on the TV I see "FIOS TV Remote DVR" as an option on one of the DVR's but I do not see that option on the other DVR which makes me wonder if they should both have the option.   Maybe if they both don't have the option then it is not activated.
    Also, if at the TV remote control I do a MENU button > Settings > FIOS TV Remote DVR > Web Access, press OK to enter Parental Control PIN.... it says "Status:  Not Activated.   To access the web site, go to http://www.verizon.com/fiostvcentral/   LoginID: <my login id> (verizon.net)    Confirmation Code: <9999999999>"   I can't figure out how to use that confirmation code for web access.  Maybe that could activate it.   Don't see anything on fios tv central that asks for it.  
    Any help on these issues would be much appreciated.     I've been through verizon tech support three times now and haven't gotten a solution.   They have remoted into my laptop to see what I see and do not have a solution.   They are escalating it at this point.

    You're going to need to describe what cables you have where a bit more clearly ...
    - Do you have a cable on the porch?  If so, where does the other end of it go?  Is it presently connected to anything?
    - The connection to the TV in the bedroom, where does the other end of that cable go?  Is it presently connected to anything?
    - There should be a box installed somewere on your house called an ONT (it's where the fiber terminates from Verizon and is converted into the cable and phone line).    The cable that comes out of that box (the coax, not the fiber), where does that run to?  Is it presently connected to anything?

  • Creating a DNS Record for a Host with Two or More IP???

    Can we create DNS A Record for a Host with Two or More IP ... ( we like to use my website  "mysite.com" pointing to two Ips )
    Please help...

    Sure, no worries.
    In a production environment DNS will query always the first record it will stores in cache, you need to find a dynamic or NLB way to achieve the automatic fail over else when you will have an outage with the first IP, then you need to ask your clients to
    clear the cache and register to DNS again, this i will not suggest in a production environment, lots of manual efforts and doesnt sound like a solution in a production environment, i would suggest you to explore windows NLB, it's easy to set and use the OS
    license.
    Thanks
    Inderjit

  • To split a table as two tables with equal no of records

    Hi Everyone,
    I have a table with size of 100000 records which is reterieved from different tables thats why idx is not in a sequential order. for example
    idx
    1
    5
    100 and so on
    i want to create two different tables with same number of records but i dont want any duplicate records. Please anyone can help how can i get it.
    Best Regards,

    Please check the following script - hope that will help you --
    SQL>
    SQL> set serveroutput on
    SQL>
    SQL>
    SQL> create table test_vaneeza
      2  as
      3   (
      4     select 1 idx,'55 Mary Road' address from dual
      5     union all
      6     select 2, '10 Glenhuntly Road' from dual
      7     union all
      8     select 3, '5 Terrace' from dual
      9     union all
    10     select 100, '1212 Dandenong Road' from dual
    11     union all
    12     select 1000, '12 Liverpool Street' from dual 
    13   );
    Table created.
    SQL>
    SQL>
    SQL> set lin 1000
    SQL>
    SQL> select * from  test_vaneeza;
           IDX ADDRESS
             1 55 Mary Road
             2 10 Glenhuntly Road
             3 5 Terrace
           100 1212 Dandenong Road
          1000 12 Liverpool Street
    SQL>
    SQL>
    SQL> drop table gghh;
    Table dropped.
    SQL>
    SQL>
    SQL> create table gghh(a  number(10),
      2                     b  varchar2(100));
    Table created.
    SQL>
    SQL>
    SQL> drop table ggyy;
    Table dropped.
    SQL>
    SQL>
    SQL> create table ggyy(c  number(10),
      2                     d  varchar2(100));
    Table created.
    SQL>
    SQL>
    SQL> select count(*) from ggyy;
      COUNT(*)
             0
    SQL>
    SQL>
    SQL> select count(*) from gghh;
      COUNT(*)
             0
    SQL>
    SQL>  insert all
      2       when rownum<=(select count(distinct idx) from test_vaneeza)/2
      3       then 
      4         into gghh
      5       else
      6         into ggyy
      7      select distinct idx,address
      8      from test_vaneeza;
    5 rows created.
    SQL>
    SQL>
    SQL> commit;
    Commit complete.
    SQL>
    SQL>
    SQL> select count(*) from ggyy;
      COUNT(*)
             3
    SQL>
    SQL>
    SQL> select count(*) from gghh;
      COUNT(*)
             2
    SQL>
    SQL>
    SQL> select * from ggyy;
             C D
             3 5 Terrace
           100 1212 Dandenong Road
          1000 12 Liverpool Street
    SQL>
    SQL>
    SQL> select * from gghh;
             A B
             1 55 Mary Road
             2 10 Glenhuntly Road
    SQL> Regards.
    Satyaki De.

  • Magic Mouse  question. I have set this in Preferences by ticking Swipe between pages with  one finger and also Swipe between full-screen apps with two fingers,  however when I try to do the latter, the 2 finger swipe will take me only to the dashboard and

    It's a Magic Mouse question. I have set this up in Preferences by ticking Swipe between pages with one finger and also Swipe between full-screen apps with two fingers,  however when I try to do the latter, the 2 finger swipe will take me from the app that I am on to the dashboard only and not to my other open apps.
    I also find that Smart zoom only works with some apps, for instance not with Word docs or email.
    How can I correct this please?

    Just figured out it also does not work with the Mail app.

  • Set up itunes account with two different credit cards

    Is it possible to set up an existing iTunes Store Account with two different credit cards?
    I am currently registered with a personal card but wish to make one purchase for work (Lion OS) using a company credit card.
    many other sites let you choose what card to pay from, multiple addresses, etc. but I don't see any options from apple site.
    for this purchase I am considering:
    1. going into the "edit payment information".
    2. editing all the info related to the company card and address.
    3. completing the purchase and download
    4. going back to the payment info and putting back my original personal informaton.
    there must be an easier way -- or is this the only apple offering.
    any advice appeciated. TIA.

    the_0ne wrote:
    but this discussion is clearly for people that have multiple accounts because they have multiple credit cards.
    This is incorrect.
    It is very clearly for people with a single iTunes account and they want to use multiple credit cards for purchases on that single iTunes account.
    If I had one iTunes account with multiple credit cards registered (which again, is what this discussion is in reference to),
    Huh? You just wrote it was NOT that.
    I bought Lion through my company, which means I logged into the App Store with my company's credentials, of which I have my company credit card registered.
    You mean your company paid for it. You are simply the one who did the purchasing using company funds.
    I now have a new iMac of which I want to install Lion.  Apple allows you to install this same install multiple times on your own personal machines.  I had to remember which account I bought Lion on so that I logged into the App Store correctly.  If I would have logged into my new iMac with my personal account, I would have then bought Lion again, of which I didn't want to do.
    But you did not purchase Lion with your personal account. It was purchased on your business account.
    If you want Lion on your persona/home computers, you will need to purchase it.
    I also wanted to install iPhoto.  I bought iPhoto on my personal account.  I had to then log out of the app store from my company account and log back in using my personal account to install iPhoto or I would have just bought iPhoto with my company's credit card, of which I would have been in a good amount of trouble.
    Imagine if I bought 3 or 4 apps a week and these apps were scattered across my business account and my personal account.  Each time I wanted to install these apps on one of my multiple machines in my  house, I would have to remember which account I bought the app on. or go to my purchases or which there could possibly be dozens of apps in each account.
    I would hope that your company requires you to track purchases you have made with their money.
    And I would think that you would want to keep track of all business purchases so you can get reimbursed (for the purchases you made on their behalf). If/when you leave the company, they can get company owned software that you purchased with their funds.

  • HT204053 If I have set up my Mac with two different users,  can I use two different Apple IDs on the same Mac?

    If I have set up my Mac with two different users,  can I use two different Apple IDs on the same Mac?

    Each user account may have its own Apple ID.

  • I was setting up my Airport and thought the first set up did not go through, so I set up again and I ended up with two accounts instead of one.  How can I manage to have only one account now? Thanks for the help.

    I was setting up my Airport and thought the first set up dod not go through, then I set up again and ended up with two wireless accounts.  I use it for the prointer and the iPad, and I can see both accounts in the iPad.  How do I get rid of one account?  Thanks for the help!

    me.com accounts can be used for iCloud.  See the FAQ section in:
    <http://support.apple.com/kb/ht4895>
    but it may be too late if you have already created a new AppleID.
    A few years ago Apple said they were working on allowing account merging, but it never happened (maybe objections from copyright holders).

  • Can I set up a WDS network with two main base stations?

    I have an AEBS in the main part of the house and a time capsule in the office in the back of the house, currently with one internet connection (it's cable modem) into to the AEBS. I have tried to use relays (two airport expresses) in a WDS to have the internet connection reach the time capsule, but unfortunately the distance is too great and it won't make it.
    I can move the internet connection and AEBS into the middle of the house and everything would be fine (one internet connection into the house, no need for relays to reach the time capsule), but this requires rewiring the cable connection and adding a power outlet, which seems like a lot of work.
    My other option is to simply have another internet connection hardwired into the office. This would be using the same cable that comes into the house and is split outside. If I go this route--my question is--can I get the time capsule and the AEBS be on the same WDS network (same name, same passwords?), or will I have two separate networks in the house? If it is the former--how do I set it up? Can I have a WDS with two main base stations? Or is there an easier route?
    Thanks the help and advice...

    My other option is to simply have another internet connection hardwired into the office. This would be using the same cable that comes into the house and is split outside. If I go this route--my question is--can I get the time capsule and the AEBS be on the same WDS network (same name, same passwords?), or will I have two separate networks in the house? If it is the former--how do I set it up? Can I have a WDS with two main base stations? Or is there an easier route?
    Just run the Ethernet cable from one of the LAN ports on the AEBS to the WAN port on the Time Capsule. Configure the Time Capsule to operate as a bridge.
    You do not use WDS when they are wired together.
    Then configure the AEBS and Time Capsule to use the same SSID, wireless encryption type/level/password. Have them operate on different channels as far apart as possible.

  • How to Insert more than one record at a time- with fixed set of values in one field

    Can someone guide as to how to insert multiple records at a time using ASP VBScript in Dreamweaver CS4 or ADDT.
    If someone can guide then the exact problem is given below.
    I have a MS access database with one table. The table has three fields. The first field is an autonumber field for ID number.
    The second field contains string values- One out of 7 predefined values. One set of records consists of 7 records containing all the seven types of predefined values in fields no 1.
    The third field is a numeric field and can contain integers.
    I want that the user can enter data for the table using an ASP form in such a way that he enters one set of records at a time in one single screen. This way he will not have to remember that he has /has not entered all the seven set of values for the field no 1.
    I am not creating fields with the 7 types of field1value as their names as it will increase the number of fields drastically.
    Please help for dreamweaver ASP VBScript.

    I have successfully inserted seven records in one form submit operation by looping through the command object execute method.
    However, the data that is being inserted by the command object is repetition of the first record and the command object is not taking any data from the text boxes for the second record onwards. In this I had used the isert record behavious generated by DW CS4 and modified it to suit my requirement. However, the data inserted in the first row is getting repeated in all the seven rows.
    Please help.
    Also advise if there are any free dreamweaver server side validation extensions available.

  • I have 2? How do I sync selected photos from my IPad to my IMac w/o losing all photos on my IPad and getting the phoos o my IMac. ou see I have two IMacs with different sets of photos. Also want to convert all Apple works docs to pages docs on my IMac.

    I have 2? How do I sync selected photos from my IPad to my IMac w/o losing all photos on my IPad which will be replaced with the photos on my IMac.You see I have two IMacs with different sets of photos. Also want to convert all Apple works docs to pages docs on my IMac. Is there and app available to do the former. many thanks. b

    The iPad and related software is designed to sync with only one computer. I suggest transferring the photos from one of your iMacs to the other so that all photos are on one Mac. Same for all other files you want to sync over.
    To get those AppleWorks files to the iPad you'll have to determine if Pages will open them. If it will then use the file sharing feature of iTunes to copy them to Pages on your pad.
    If Pages will not open the AppleWorks files directly you'll have to save them in some format that Pages will read. These include but may not be limited to Pages and Word formats.

Maybe you are looking for

  • Upgraded drive, now Lightroom can't find my main catalogue

    I have a separate data drive in my system. When it filled up, I upgraded to a larger data drive. The person who set it up for me transferred all the dotage to the new drive. Now Lightproof can't find my main catalogue although for some reason my coll

  • Castor xml data binding

    Hi, i try to use castor with jdev so i put castor-0.9.5.3.jar in my libraries. But when i try to use the unmarshaller i got this error : java.lang.NoClassDefFoundError: org.exolab.castor.util.LocalConfiguration      at org.exolab.castor.xml.Unmarshal

  • Having trouble installing the latest flash player.

    Hey, I'm trying to install Flash Player 8, but whenever I go to the Install Icon on my desktop, it comes up with the message "Setup could not find your browser's plugins directory". What is this directory and where do I find it or where can I get it.

  • How do I import RAW photos into iPhoto?  I cannot import RAW photos from my Olympus EPL-2 camera.  I am using iPhoto 7.1.5.

    I cannot import RAW photos from my Olympus EPL-2 camera.  I am using iPhoto 7.1.5.  I read that iPhoto has to update their system and add each camera to be able to import RAW photos from it.  If this is true, how can I let Apple know to add my camera

  • Can I copy 10.4.2. DVD to external drive?

    I buy the new OS but didn't believe they would sell it wothout a CD version. On a system with a DVD, can I copy the installation DVD to an external drive then use that to upgrade my CD only system? I bought an IPOD Nano but it needs a system with 10.