Single row from one-to-many

I am having trouble with a query, I need a single row from a 1-to-many relationship that is prioritized by certain ID's.
Department Table - dept_id,dept_name
Employee Table - emp_id, emp_name
D-E-Lookup Table - dept_id,emp_id
Employee ID priority are 5,8,9,21,33,78
I need each department name with the associated employee name.
There should be only one row per department.
If that department does not have empID 5, than 8, if not 8 then 9, if not 9 than 21, etc
Any help would be greatly appreciated.

How about this:
WITH departments AS
    ( SELECT ROWNUM AS deptno, COLUMN_VALUE AS dname
      FROM   TABLE(sys.dbms_debug_vc2coll('Sales','IT','Research')) )
   , employees AS
    ( SELECT ROWNUM AS empno, COLUMN_VALUE AS ename
      FROM   TABLE(sys.dbms_debug_vc2coll('Bennett','Parkman','Nakamura')) )
   , department_assignments AS
     ( SELECT 1 AS deptno, 1 AS empno, 5 AS priority FROM dual UNION ALL
       SELECT 1 AS deptno, 2 AS empno, 8 AS priority FROM dual UNION ALL
       SELECT 1 AS deptno, 3 AS empno, 9 AS priority FROM dual UNION ALL
       SELECT 2 AS deptno, 2 AS empno, 8 AS priority FROM dual UNION ALL
       SELECT 2 AS deptno, 3 AS empno, 9 AS priority FROM dual UNION ALL
       SELECT 3 AS deptno, 1 AS empno, 9 AS priority FROM dual UNION ALL
       SELECT 3 AS deptno, 3 AS empno, 21 AS priority FROM dual )
SELECT dname, ename, priority
FROM   ( SELECT d.dname
              , e.ename
              , dp.priority
              , DENSE_RANK() OVER (PARTITION BY d.deptno ORDER BY dp.priority) AS ranking
         FROM   departments d
                LEFT JOIN department_assignments dp ON dp.deptno = d.deptno
                LEFT JOIN employees e ON e.empno = dp.empno )
WHERE ranking = 1;
DNAME         ENAME             PRIORITY
Sales         Bennett                  5
IT            Parkman                  8
Research      Bennett                  9
3 rows selectedThe <tt>WITH</tt> clause is just to define test data inline rather than creating tables, in case that's not clear.

Similar Messages

  • Importing single row from an exported dump of many tables

    Hi all,
    I have a requirement to import a single row from a exported dump which has collection of tables in it.
    i have used export of a single row and import of single row(from single row export dump), now i need to import a single row from a dump containing collection of tables...
    kindly help me out with this.

    971424 wrote:
    Hi all,
    I have a requirement to import a single row from a exported dump which has collection of tables in it.
    i have used export of a single row and import of single row(from single row export dump), now i need to import a single row from a dump containing collection of tables...
    kindly help me out with this.post command line that produced the dump file.
    How do I ask a question on the forums?
    SQL and PL/SQL FAQ

  • Retrieve a single row from Oracle function

    Hi!
    I have an Oracle function created as follow:
    CREATE OR REPLACE FUNCTION user_data(userId IN users.user_id%TYPE)
    RETURN users%ROWTYPE AS
    userData users%ROWTYPE;
    BEGIN
    SELECT * INTO userData
    FROM users
    WHERE user_id = userId;
    RETURN userData;
    END user_data;
    This function returns a single row from 'users' table.
    I tried to retrieve that single row by mean of:
    CallableStatement statement = connection
    prepareCall("{ call ? := get_data(?) }");
    statement.*registerOutParameter(1, OracleTypes.OTHER)*;
    statement.setInt(2, 103);
    statement.execute();
    ResultSet rs = (ResultSet) statement.getObject(1);*
    String value = rs.getString(2);
    System.out.println(value);
    But this code doesn't work.
    I tried other OracleTypes, also. (I don't know what OracleType I receive when the Oracle function return a ROWTYPE.)
    Can somebody tell me how to retrieve that single row?
    What is the type of out parameter (registerOutParameter()) when the Oracle function return a ROWTYPE?
    Notes: No cursor can be added. No database change is allowed.
    Thank you in advance.
    [Adrián E. Córdoba]
    Edited by: aecordoba on Mar 18, 2011 3:58 PM

    aecordoba wrote:
    I beg your pardon for my bad English. (It isn't my original language.)
    It's not a language problem. It's that you didn't provide any details about what went wrong.
    That just is my problem:
    I know the retrieved result is not a result set: It's a single row.Doesn't matter if it's a single row. You have a ResultSet object. You have to call ResultSet's next() method to get to the first row, even if it's the only row.
    1- Which is the Oracle type I receive in Java when the Oracle function returns a ROWTYPE?I don't know.
    2- How can I get each column value when I receive a single row in Java?The same way as when there are multiple rows: For each row, call ResultSet.next() to advance to the next row, then call the appropriate ResultSet.getXxx() methods to get each column's value. Again: If you have a ResultSet, you must call next() to get to the first row, even if there is only one row. "First row out of 1 total row" is no different than "first row out of 100 total rows."
    EDIT: Okay, I didn't notice before that you're using a CallableStatement with "out" parameters. I've never used one of those before, so I'm not familiar with the details. I really don't know if casting to a ResultSet is appropriate here. Do you actually have documentation that says you can do that, or are you just guessing and trying to find something that works.
    Edited by: jverd on Mar 18, 2011 11:26 AM

  • Passing Multiple table row from one view to another view

    Hi,
    How to Passing Multiple table row from one view to another view in Web Dynpro Abap. (Table UI Element)
    thanx....

    Hi Ganesh,
    Kindly do search before posting.. this discussed many times..
    First create your context in component controller, and do context mapping in two views so that you can get values from
    one veiw to any views.
    and for multiple selection, for table we have property selection mode.. set as multi and remember context node selection
    selection cardinality shoud be 0-n.
    so, select n no of rows and based on some action call sec view and display data.( i think you know navigation between veiw ).
    Pelase check this...for multi selection
    Re: How to copy data from one node to another or fromone table to another table
    for navigation.. check
    navigation between the views
    Cheers,
    Kris.

  • Select a single row from a billion row table

    This is a fictitious scenario, how would you write a select statement on a table with a billion rows. It never returns anything,right? Somebody was suggesting a stored procedure.
    As an example : Assuming a Table with columns      Account(int), TransDate(DateTime), TransNum(int) and few other columns. I need a transaction that happened on 03-05-2014 8:15PM. Clustered index on Account. Non- clustered on TransDate.
    I was suggested to create a stored procedure, inside the SP you have 3 parameters: min_date, max_date, avg= min_date+max_date/2. You create a loop and feed the avg value to the max_date or min_date depending on where the row is. This is a suggestion
    that I am not clear my-self but wanted to see if you guys can help me develop this idea.
    Also please suggest how you would do it in your world. You guys could have much better ideas probably much simpler one's. Thanks in advance.
    svk

    I basically just need transaction for one particular datetime. Not any span. One of our senior developers suggested that a simple select statement takes for ever to return a single row from a billion rows and suggested a vague idea as above. 
    Either there is a suitable index on the column, and the SELECT will be fast.
    Or there is no index on the column, and in that case it will take quite some time to find the row. The only reason to loop is that you don't want to take out a table lock, but in that case you would do something like looping one account at a time. Looping
    over different time values will only mean that you will scan the table multiple times.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Retrieve a single row from Multirow ResultsetObject

    I would like to know how can i retrieve a single row from a multi row result object. I have a result object which retrieved multiple rows and i would like to retrieve only the top row or a specific row. Can any one of you suggest how this can be done. Thank you for all the help in advance.

    call next() on your result set, this goes to the first row and you can retrieve the data with getString(), getInt(), etc.

  • Deleting single row from Special Price for Business Partners

    Hi Experts,
    How would I go about about deleting a single row from Special Prices for Business Partners across all BPs when for most of my BPs this is the only special price they have.  I have tried using Copy Discounts function but once you are down to 0 rows the record has effectively gone from the table so there is nothing to copy. 
    Hopefully I won't have to go thorugh 5000 records and delete each one idividually.
    Thanks
    Jon

    Hi Jon,
    You may try a tool called B1TCH:
    /people/community.user/blog/2007/08/19/get-your-kicks-with-di-commander
    Thanks,
    Gordon

  • UPDATE single ROW from DATAGRID without refreshing the entire ItemsSource

    Hello there,
       I have a simple datagrid that has an ItemsSource of ObjectQuery<DbDataRecord>.
       Let's say I want to refresh a single row from the datagrid, (Because a specific item i know has been changed) without refreshing the entire ItemsSource because the source query is quite big.
    Any ideas?
    Thanks.
    -- Jorge_M_P

    Whilst you could raise property changed on every field in an item, that would of course mean implementing inotifypropertychanged on a wrapper object, then iterating all of those properties.
    An observablecollection implements the INotifyCollectionChanged interface.
    Amongst the possibilities for changes which that notifies are this one:
    https://msdn.microsoft.com/en-us/library/ms653207(v=vs.110).aspx
    Which notifies of a single item change.
    I usually wrap my entity framework objects.
    Sometimes I wrap all the properties so I can do change tracking in the viewmodel.
    This is a technique similar to the one I use in this sample:
    https://gallery.technet.microsoft.com/WPF-Highlight-Changed-a77976d4
    Which wraps plain classes - but the principle is the same.
    And
    I wrap the object and expose that, as I do in this:
    http://social.technet.microsoft.com/wiki/contents/articles/28209.wpf-entity-framework-mvvm-walk-through-1.aspx
    Note that because I wrap the same objects returned from EF, there is very little overhead in using an observable collection there.
    Hope that helps.
    Recent Technet articles:
    Property List Editing ;  
    Dynamic XAML

  • Copying table rows from one table to another table form

    Hi
    I have a problem about Copying table rows from one table to another table form.On jsf pages if you enter command button go anather jsf page and it copy one row to another table row. But when i execute this process for table FORM it doesn't copy I wrote a code under "createRowFromResultSet - overridden for custom java data source support." Code block is:
    ViewRowImpl value = super.createRowFromResultSet(qc, resultSet);
    try{
    AdfFacesContext fct = AdfFacesContext.getCurrentInstance();
    Number abc = (Number)fct.getProcessScope().get("___");
    value.setAttribute("___",abc);
    }catch(Exception ex){System.out.println(ex);  }
    return value;

    Table may be copied with the
    expdp and impdp utilities.
    http://www.oracle.com/technology/products/database/utilities/index.html

  • HOW TO GET THE SELECTED VALUE IN A ROW FROM ONE VIEW TO ANOTHER VIEW?

    hi all,
    I  have a small issue.
    i have created two views.In the table of the first view i'm selecting a row and pressing the button it will move to next view.
    i am adding some fields manually in the table of the second view and pressing the save button.Here all the values should get updated corresponding to the field which i have selected in the first view.
    I want to know how to get the particular field in the selected row from one view to another view.
    Kindly help me.

    Hi,
            Any data sharing accross views can be achiveved by defining CONTEXT data in COMPONENT CONTROLLER and mapping it to the CONTEXT of all the views. Follow the below steps.
    1. Define a CONTEXT NODE in component controller
    2. Define same CONTEXT NODE in all the views where this has to be accessed & changed.
    3. Go to CONTEXT NODE of each view, right click on the node and choose DEFINE MAPPING.
    This is how you map CONTEXT NODE and same can be accessed/changed from any VIEW or even from COMPONENT CONTROLLER. Any change happens at one VIEW will be automatically available in others.
    Check the below link for more info regarding same.
    [http://help.sap.com/saphelp_nw04s/helpdata/EN/48/444941db42f423e10000000a155106/content.htm]
    Regards,
    Manne.

  • Trying to copy a single page from one Adobe Muse site to another Adobe Muse site?

    Is there a way to copy a single page from one site to another within Muse (both muse sites)?  What exactly does the "Export Page" direction do?  I can't seem to determine how to "import" that page?

    Hi
    By default there's no automated way to move page from one site to another within Adobe Muse. However, you may manually copy the content of the page and paste in place on the new page you created in another  muse site.
    You may want to look to this short video how you may copy the content and paste in place :   http://screencast.com/t/lULpqvGCt
    As far as your another query is concerned, the export page option you see as shown below :
    is used when you publish the site to the hosting service and if you un-check this option the page won't be uploaded to the hosting service or won't be exported.
    I hope this information helps. Let me know if you have any further query related to this.
    Regards
    Anshul

  • Passing Multiple rows from one external webpart list to another

    Hi Folks,
                    I have almost spent 1 week looking into this without any success. I have an external list "List A"  (in a webpart) with one of the columns as "State".
    Another external list "List B"  (in a webpart ) has state and user as columns. In some case I want to pass 1 state and in another I want to pass multiple state.  Passing one state from List A to List B works fine. But Multiple state does
    not work because the webpart list has the property "Send First row to connected web parts when page loads".  If I disable this option then the web part does not pass anything. Is there a way to pass multiple rows from one Webpart
    external list to other?

    http://www.sharepointanalysthq.com/2010/07/bcs-external-list-limitations/
    No Lookups
    Unfortunatly the only thing that you can do a look up on in an external list is on the ID column, anything else and you are out of luck.
    http://social.technet.microsoft.com/Forums/en-US/615771a0-ba78-4e38-9e2d-ded0204173ba/external-list-referenced-as-sharepoint-lookup?forum=sharepointgeneralprevious
    Try below webpart. it should help
    http://www.sparqube.com/SharePoint-Lookup-Column/
    If this helped you resolve your issue, please mark it Answered

  • Single row from this query without create a group by

    Can I have a single row from this query without create a group by on tipo (TIPO can have only 2 value (A,D)
    SELECT
    CASE TIPO
    WHEN 'D' THEN SUM(IMPORTO) ELSE 0 END DIMPORTO,
    CASE TIPO
    WHEN 'A' THEN SUM(IMPORTO) ELSE 0 END AIMPORTO
    FROM MGIORNALE
    WHERE K_CONTO = '100001' --CONTO
    AND DECODE(T_MOVIM,'MRAP',TO_DATE('31/12/'||to_char(a_competenza),'DD/MM/YYYY'),DATA_RG)
    -- BETWEEN DATAA AND DATAB
    BETWEEN '01/01/2006' AND '31/12/2006'
    --GROUP BY TIPO
    --AND TIPO = COL_conto
    Thanks in advance

    Is like this?
    sum (CASE TIPO
    WHEN 'D' THEN IMPORTO ELSE 0 END) DIMPORTO,

  • Selecting a single row from table control of standard transaction via repor

    Hi Experts,
    I have a requirement of selecting a single row from standard trasaction via ineractive report.
    For eg. for a given document number & item number, how can i select the specified item from transaction VA03.
    I am using call transaction to naviagate to the screen but unable to select the specified item.
    thanks in adavance for your Help.

    You mean selecting the item via BDC?
    Have you tried something like:
    perform bdc_field       using 'BDC_CURSOR'
                                  'VBAP-POSNR(01)'.
    perform bdc_field       using 'RV45A-VBAP_SELKZ(01)'
                                  'X'.
    or whatever your dynpro is to select the first row?

  • Delete single row from DBTAB

    Hi Experts,
    I want to delete a single row from DBTAB ( KLAH )
    WHERE KLAHKLART = '2'  and  KLAHCLINT = '241'.
    Please advice
    Karthik

    Hi ,
    Try this code  To delete single record from DB table- -
    DATA : t_klah LIKE TABLE OF klah,
           fs_klah LIKE klah.
    SELECT * FROM klah INTO TABLE t_klah
                   WHERE klart = '2' AND
                         clint = '241'.
    LOOP AT t_klah INTO fs_klah.
      DELETE klah FROM fs_klah .
      EXIT.
    ENDLOOP.
    Regards
    Pinaki

Maybe you are looking for

  • Office Web Apps 2013 + could not establish trust relationship

    We currently have a three tier SharePoint 2013 Farm: 1. Web Front End Server (Server 2008 R2 Enterprise) - Servername: TEST2SP013.domain.dom 2. Central Admin Server (Server 2008 R2 Enterprise) - Servername: TEST2SPCA013.domain.dom 3. SQL Server (Serv

  • Bug in servlet mapping

              Hi, it appears that the servlet mapping specified in section 10 of the           Servlet 2.2 spec is broken in Weblogic server 6.0 GA. Here is the servlet           2.2. spec for mapping:           10.1 Use of URL Paths           Servlet co

  • I can only read on my external usb hardrive

    Hello There, I could need some help When I plug my "Samsung SP1604N Media Mass-Storage-Device" to my (brand new :-)) iBook, i'm only "allowed" to read but not to write on the single volume, that is formatted in Windows NTFS format. First, I thought I

  • Cpu load at half 100c

    With my mac at 45 percent cpu load my mac will hit 100c on left for dead 2 before it has the time to speed up the fans, when the fans speed up it will lower it to 92-98c is this normal

  • User Profile Photo

    THis is kind of a stupid question but I need the help anyway. When I first setup my MacBook Pro, it snapped a profile picture. My son was sitting in front of the computer and it was his picture that was used as my profile pic. Somehow the picture was