date max with multiple joins of tables

Looking for expert advice on the use of max (date) with multiple joins of tables. Several people have tried (and failed) - HELP Please!

The goal is to retrieve the most current joined line of NBRJOBS_EFFECTIVE_DATE for each unique NBRJOBS_PIDM. There are several lines by PIDM with various EFFECTIVE_DATEs. The following SQL returns about 1/3 of the files and there are also some multiples.

The keys are PIDM, POSN and suff

Select NBRJOBS. NBRJOBS.*,
NBRBJOB. NBRBJOB.*
of POSNCTL. Inner join of NBRBJOB NBRBJOB POSNCTL. NBRJOBS NBRJOBS on (NBRBJOB. NBRBJOB_PIDM = NBRJOBS. NBRJOBS_PIDM) and (NBRBJOB. NBRBJOB_POSN = NBRJOBS. NBRJOBS_POSN) and (NBRBJOB. NBRBJOB_SUFF = NBRJOBS. NBRJOBS_SUFF)
where NBRJOBS. NBRJOBS_SUFF <>'LS '.
and NBRBJOB. NBRBJOB_CONTRACT_TYPE = 'P '.
and NBRJOBS. NBRJOBS_EFFECTIVE_DATE =
(select Max (NBRJOBS1. NBRJOBS_EFFECTIVE_DATE) as 'EffectDate '.
of POSNCTL. NBRJOBS NBRJOBS1
where NBRJOBS1. NBRJOBS_PIDM = NBRJOBS. NBRJOBS_PIDM
and NBRJOBS1. NBRJOBS_POSN = NBRJOBS. NBRJOBS_POSN
and NBRJOBS1. NBRJOBS_SUFF = NBRJOBS. NBRJOBS_SUFF
and NBRJOBS1. NBRJOBS_SUFF <>'LS '.
and NBRJOBS1. NBRJOBS_EFFECTIVE_DATE < = to_date('2011/11/15','yy/mm/dd'))
order of NBRJOBS. NBRJOBS_PIDM

Welcome to the forum!

We don't know what you are trying to do.
You want all of the columns in the rows where NBRJOBS_EFFECTIVE_DATE is the date limit before a given date (November 15, 2011 in this example) for all rows in the result set with this NBRJOBS_PIDM? If so, here is one way:

with         GOT_R_NUM     as
(
     select       NBRJOBS.NBRJOBS.*,
            NBRBJOB.NBRBJOB.*     -- You may have to give aliases, so that every column has a unique name
     ,       rank () over ( partition by  NBRJOBS.NBRJOBS_PIDM
                               order by      NBRJOBS.NBRJOBS_EFFECTIVE_DATE     desc
                      )             as R_NUM
     from          POSNCTL.NBRBJOB NBRBJOB
     inner join      POSNCTL.NBRJOBS NBRJOBS       on    (NBRBJOB.NBRBJOB_PIDM = NBRJOBS.NBRJOBS_PIDM)
                                        and      (NBRBJOB.NBRBJOB_POSN = NBRJOBS.NBRJOBS_POSN)
                                  and      (NBRBJOB.NBRBJOB_SUFF = NBRJOBS.NBRJOBS_SUFF)
     where       NBRJOBS.NBRJOBS_SUFF             != 'LS'       -- Is this what you meant?
     and        NBRBJOB.NBRBJOB_CONTRACT_TYPE   ='P'
     and       NBRJOBS.NBRJOBS_EFFECTIVE_DATE  <= to_date ('2011/11/15', 'yyyy/mm/dd')
)
select       *     -- Or list all columns except R_NUM
from       GOT_R_NUM
where       R_NUM          = 1
order by  NBRJOBS_PIDM
;

Normally this site does not display the <>inequality operator; He thinks it's some kind of beacon.
Whenever you post on this site, use the other inequality operator (equivalent), *! = *.

I hope that answers your question.
If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) for all the tables involved and the results desired from these data.
Explain, using specific examples, how you get these results from these data.
Always tell what version of Oracle you are using.
You will get better results faster if you always include this information whenever you have a problem.

Tags: Database

Similar Questions

  • BAD RESULTS WITH OUTER JOINS AND TABLES WITH A CHECK CONSTRAINT

    HII All,
    Could any such a me when we encounter this bug? Please help me with a simple example so that I can search for them in my PB.


    Bug:-8447623

    Bug / / Desc: BAD RESULTS WITH OUTER JOINS AND TABLES WITH a CHECK CONSTRAINT


    I ran the outer joins with check queries constraint 11G 11.1.0.7.0 and 10 g 2, but the result is the same. Need to know the scenario where I will face this bug of your experts and people who have already experienced this bug.


    Version: -.
    SQL> select * from v$version;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE    11.1.0.7.0      Production
    TNS for Solaris: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production

    Why do you not use the description of the bug test case in Metalink (we obviously can't post it here because it would violate the copyright of Metalink)? Your test case is not a candidate for the elimination of the join, so he did not have the bug.

    Have you really read the description of the bug in Metalink rather than just looking at the title of the bug? The bug itself is quite clear that a query plan that involves the elimination of the join is a necessary condition. The title of bug nothing will never tell the whole story.

    If you try to work through a few tens of thousands of bugs in 11.1.0.7, of which many are not published, trying to determine whether your application would be affected by the bug? Wouldn't be order of magnitude easier to upgrade the application to 11.1.0.7 in a test environment and test the application to see what, if anything, breaks? Understand that the vast majority of the problems that people experience during an upgrade are not the result of bugs - they are the result of changes in behaviour documented as changes in query plans. And among those who encounter bugs, a relatively large fraction of the new variety. Even if you have completed the Herculean task of verifying each bug on your code base, which would not significantly easier upgrade. In addition, at the time wherever you actually performed this analysis, Oracle reportedly released 3 or 4 new versions.

    And at this stage would be unwise to consider an upgrade to 11.2?

    Justin

  • Indicator of waveform data dashboard with multiple locations

    I have a chart in waveform with multiple locations on my main VI running.

    I use the Application Dashboard data 2.2.1 on my Ipad to monitor table of wave shape of my running app.  I placed a marker on my Ipad and also related waveform with my waveform array variable.  I can't play my data dashboard application because it shows unable to connect to the server.  I noticed that it is because of multiple plots being plotted on my table of waveform.

    I could only play the App data dashboard when it is just a single parcel related to an indicator on the App.

    My question is if it's possible to have a graphical indicator of waveform on a data dashboard that has several plots being plotted and not only a single square, and how to configure it?

    Thank you.

    Click on Bravo and select as an accepted answer.  You are welcome.

  • Select Date Max with more than one result

    Hello;

    I need to write a query that returns the maximum date for a specific transaction that is not a problem, but my problem occurs when I have several transactions with the same date max trans.


    Can someone help me write something which will return only one record from a result set that has more than one record

    Example:

    TRAN_ID - LAW #-TRANS_DATE
    -272-1111111-29/10/1999 00:00:00
    -273-1111111-29/10/1999 00:00:00
    -274-1111111-29/10/1999 00:00:00
    -275-1111111-29/10/1999 00:00:00

    You are referring to your alias at the wrong level, try this:

    select * from (
    select account_transaction_id,
           account_number,
           max_days_in_arrears,
           trans_date,
           row_number() over(partition by account_number order by trans_date desc, account_transaction_id desc) as rn
      from account_transaction atr
     where max_days_in_arrears > 90
       and account_number in (100026, 121189, 100223)
       and trans_date = (select distinct max(trans_date)
                           from account_transaction
                          where account_number = atr.account_number
                            and max_days_in_arrears > 90)
     order by account_number, trans_date
    )
    where rn = 1;
    

    and use code tags please, so that your queries can still be read easily and quickly for us to understand.

    Published by: hoek on March 23, 2009 15:48

  • Max() with outer join

    I have a couple of tables XX_PERS and XX_ADDRESS as shown below. I would like to return more recent 'city' for each person on the xx_pers table. If no record matching in xx_address, there should always be the id of the person, name of city as null.

    CREATE TABLE xx_pers

    (Key primary ID number,)

    name varchar2 (100)

    );

    CREATE TABLE xx_address

    (

    addr_id PRIMARY KEY NUMBER,

    pers_id NUMBER,

    City VARCHAR2 (25).

    FOREIGN KEY (pers_id) REFERENCES xx_pers (id)

    );

    insert into xx_pers values (1, 'Messi');

    insert into xx_pers values (2, 'Muller');

    insert into xx_pers values (3, 'Ronaldo');

    insert into xx_address values (100.1, "Argentina");

    insert into xx_address values (102.1, "Madrid");

    insert into xx_address values (101.2, "Germany");

    insert into xx_address values (103.2, "Munich - OF");

    The following query returns the most recent 'city' for person 1 and 2 but do not know how to get the 3rd person.

    SELECT ID, name, city

    of xx_pers xx

    , xx_address y

    Where xx.id = y.pers_id

    and y.addr_id in (select max (addr_id) of the xx_address by pers_id group);

    Hello

    Here is another way, which is similar to what you have tried initially, but it uses an outer join to a subquery instead of an IN subquery:

    WITH got_max_addr_id AS

    (

    SELECT pers_id

    MAX (addr_id) AS max_addr_id

    OF xx_address

    GROUP BY pers_id

    )

    SELECT p.id

    p.name

    a.city

    OF xx_pers p

    LEFT OUTER JOIN got_max_addr_id ON m.pers_id = p.id m

    A LEFT OUTER JOIN xx_address ON a.pers_id = m.pers_id

    AND a.addr_id = m.max_addr_id

    ORDER BY p.id

    ;

    It is also very similar to the solution in response #2 above.

    Note Although all 4 of these solutions something in common that is different from the query you posted: they use all of the outer joins.  You obviousy has estimated that an outer join outer join would be useful here, because you mentioned an outer join in the title of the thread, but you are not one yourself.

  • How to create the data store with multiple LUNS

    Someone could please tell me that how can I have multiple LUNS in a single data store, is it possible?

    See the "Storage management" section in the document provided in the previous comment. You are probably looking for information on page 104.

  • Sharing of Data Source with multiple components

    I have a collection of ArrayCollection of time intervals that I want to use as the source for a series of ComboBoxes in several separate components.

    What is the best way to implement this?

    In fact, you can have global data objects. Since Flex is manufactured with components, it's kind of deprecated. But if create you a collection of public register in your home page, you can access it via the name of Application.application.var. When you type Application.application. It will show you what is available

  • ODI 11 g multiple table with union join

    Hi all

    I have a problem with joining 4 different tables and union all of them.

    Here I want to generate this query;

    Select * from

    Table1, table2

    Join table2 = table1

    Union of all the

    Select * from

    Table 3, table 4

    Join table3 = table 4

    But I have not the same column that all the table, some are missing. When I write one - PL/SQL query I can easily add a false "NULL column1' column, but I can not how I do with ODI 11 g.

    I tried not to use "Active Mapping", but it does not work.

    I'm newbie ODI 11 g

    Thank you

    Myra

    Hi, Myra,.

    I'd even as in PL/SQL. In your table target add all the columns you need and overall data whatever you're missing some columns just put NULL in the mapping.

    Thank you

    Ajay

  • Create view opaque in OBIEE with multiple tables

    Hello

    I need assistance with the opaque view since I've never created in the past. I want to create an opaque view that retrieves data from several tables. Is it possible to do? Or y at - it another alternative available in OBIEE option? I read the blog following and other similar articles on views opaque but found no option to use for the query with several tables:

    Oracle Business Intelligence: A physical layer to OBIEE 10 g Opaque View Creation


    Also please advice how to join two tables from different databases in OBIEE?


    Kindly advice.


    Kind regards

    Andy

    Check also, it may be useful to you

    http://obinsight.blogspot.com/2010/05/reporting-against-multiple.html

  • outer join while selecting the date max

    Hello
    Oracle 11g R2.

    I want to build a query that joins outside between 2 related tables, but selects only the date max if more than 1 record returned for a given customer. Here are the tables I and some records.

    create table customer (NumCli number, name varchar2 (100))
    create table orders (ordnum number, NumCli number, date orddate)

    insert into customers values (1, 'ABC')
    insert into customer values (2, 'DEF')
    insert into customer values (3, 'ABC')
    insert into customer values (4, 'DEF')

    insert into orders values (1, 1, sysdate)
    insert into orders values (2, 1, sysdate + 1)
    insert into orders values (3, 2, sysdate + 2)

    I created the following query, but it returns the two dates for NumCli = 1. I want to display the name of the client no matter if he has a prescription. But if there is more order, I want display the folder with the date max.

    Select a.custnum, b.SID, b.orddate
    client a, b of orders
    where a.custnum = b.custnum (+)
    and b.orddate (+) < = sysdate + 10 - additional criteria as part of the edition

    It is a simplified example of a more complex query, that I am trying to solve. If possible, could you please suggestion a request without the use of analytical functions or inline views?
    Thank you!!

    Published by: arizona9952 on April 4, 2013 22:05

    arizona9952 wrote:
    Thanks Jac. Any way to do this without a group of?

    Is there a particular reason not to use MAX?

    It will be difficult to find a maximum date without a group of... If it's ok for you to have everything available, you can use the correlated sub query...

    select a.custnum, a.name,
             (
               select b.orddate
               from   orders b
               where b.custnum = a.custnum
               and b.orddate <= sysdate + 10
               and rownum = 1 ) ordddate
    from customer a
    
  • Update statement with joins of tables and where Clause

    Hi, I have MS SQL background and I try to execute an update statement in Oracle with joins of tables. However, the syntax below does not work but I think it works for MS SQL.

    Basically, the base table must be attached to a master table trend with monthly snapshots, an account will be only an entry for a given date monthly. Where clause must be limited to accounts within a certain range of interest rates.

    The first approach returns command SQL ORA-00933 not correctly completed, and the second approach returns ORA-01427 row below query returns multiple rows. Can anyone help? Thanks in advance!



    1:

    Update PenaltyAll
    Set a.indicator = month (b.)
    of PenaltyAll an inner join Master b on a.acctno = b.accountnumber
    where a.monthend='01/31/2009' and b.date='12/31/2008' and b.apr < 20

    2:

    Update PenaltyAll
    adjustment indicator =
    (select to_char (b., 'MM')
    of PenaltyAll an inner join Master b on a.acctno = b.accountnumber
    "where to_char (a.monthend,'mm/dd/yyyy ') = 31 January 2009"
    (et to_char(b.date,'mm/dd/yyyy') = December 31, 2008 "
    and b.apr < 20)

    Published by: sqlrookie on August 21, 2009 07:04

    I edited my post, that was my mistake, ANC you try now?

  • Gears - error when you try to insert values into a table with multiple columns

    Hello

    I started playing with the gears and SQlLite today and I get an error when I try to insert values into a table with multiple columns.

    I have:

    var db = google.gears.factory.create('beta.database');
        db.open('developerSet');
        db.execute('create table if not exists Developers (DeveloperName text, DeveloperAge int)');
    
        var devName = "Davy"
        var devAge = 32;
    
        try {
            db.execute('insert into Developers values (?, ?)', [devName, devAge]);
            alert('success');
        }
        catch (e) {
            alert(e);
        }
    

    I get the error:

    net.rim.device.api.database.DatabaseException; insert into developers values (?,?): SQL logic error or missing database.

    I use this reference: http://code.google.com/apis/gears/api_database.html

    Everything works if I have only one field as:

    var db = google.gears.factory.create('beta.database');
        db.open('developerSet');
        db.execute('create table if not exists Developers (DeveloperName text)');
    
        var devName = "Davy"
        var devAge = 32;
    
        try {
            db.execute('insert into Developers values (?)', [devName]);
            alert('success');
        }
        catch (e) {
            alert(e);
        }
    

    I use the plug-in Visual Studio 2.0 for 2008 that are running Windows XP SP and Simulator 2.13.0.56

    Thank you

    Davy

    Yes, a SQLite database will persist between battery pulls.  The database is registered either to internal MEM or removable media (not the device memory), depending on which is available on your device.

    In general, its not considered a best practice to remove your table as soon as it is empty and re - create it again when you want to add data.  This adds extra overhead fresh for the final, delete and insert first for a given table.  Instead, define and finalize your drawing before you create your table.  Once created, review the static schema.

    That being said, for development purposes, it may be easier to provide an easy way to drop your tables while you develop your schema.

    See you soon,.

    Adam

  • How to export data to excel that has 2 tables with the same number of columns and the column names?

    Hi everyone, yet once landed upward with a problem.

    After trying many things to myself, finally decided to post here...

    I created a form in form builder 6i in which clicking on a button, the data gets exported to the excel sheet.

    It works very well with a single table. The problem now is that I cannot do the same with 2 tables.

    Because the tables have the same number of columns and the columns names.

    Here are the 2 tables with column names:

    Table-1 (MONTHLY_PART_1) Table-2 (MONTHLY_PART_2)
    SL_NOSL_NO
    MODELMODEL
    END_DATEEND_DATE
    U-1U-1
    U-2U-2
    U-4U-4
    ..................
    ..................
    U-20U-20
    U-25U-25

    Given that the tables have the same column names, I get the following error :

    402 error at line 103, column 4

    required aliases in the SELECT list of the slider to avoid duplicate column names.

    So how to export data to excel that has 2 tables with the same number of columns and the column names?

    Should I paste the code? Should I publish this query in 'SQL and PL/SQL ' Forum?

    Help me with this please.

    Thank you.

    Wait a second... is this a kind of House of partitioning? Shouldn't it is a union of two tables instead a join?

    see you soon

  • Update statement with join other tables

    Hello
    I have two table is one that contains the xml file, basically, I need to read the xml file, then upgrade to another table based on certain conditions.
    UPDATE TRCB_XBRL_STG_2 STG 
    SET PROFIT = 
      case when xbrl.isconsolidatedacc='Y' and EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..."') is not null
      THEN EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..."')
      WHEN XBRL.ISCONSOLIDATEDACC='N' AND EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..') IS NOT NULL
      THEN extractValue(XBRL.xbrlfile,'//PROFIT ', 'xmlns:acra=".."')
      ELSE STG.PROFIT
      END,
      SET REVENUE= 
      case when xbrl.isconsolidatedacc='Y' and EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE', 'xmlns:acra="..."') is not null
      THEN EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE.', 'xmlns:acra="..."')
      WHEN XBRL.ISCONSOLIDATEDACC='N' AND EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE', 'xmlns:acra="..') IS NOT NULL
      THEN extractValue(XBRL.xbrlfile,'//REVENUE', 'xmlns:acra="REVENUE"')
      ELSE STG.REVENUE
      END,
      ....
      ... (around 100 columns)
    FROM  TRCB_XBRL xbrl ,TRCB_XBRL_STG_2 STG 
    WHERE STG.XBRL_ID = XBRL.XBRL_ID 
    About 100 the number of columns, please someone suggest how to use the update with a join of two tables.

    Hello

    If all the values needed to update a given line from table_x come from the same line of table_y (or in the same row of a result set of a query on a number any of tables), then you can do something like this:

    UPDATE  table_x  x
    SET     (col1, col2, col3, ...)
    =     (
             SELECT  NVL (y.col1, x.col1)
             ,         NVL (y.col2, x.col2)
             ,         NVL (y.col3, x.col3)
             FROM    table_y  y
             WHERE   x.pkey   = y.expr
             AND         ...
         )
    WHERE   ...
    ;
    

    If the WHERE clause is based on the same line of table_y, then it will be probably easier and more efficient to use the MERGER instead of UPDATE.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    In the case of a DML (UPDATE), for example, the sample data should show what looks like the tables before the DML, and the results will be the content of the or the tables changed after the DML.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

  • Combine data sources with different granularity in the same fact table?

    I have two operating tables 'Incident (157 columns)' and 'unit (70 Colums) '. For all the "incidents" happening there could be one or more records in the table of the 'unit '.

    As part of my design of data mart, I have merged the tables in one "makes the incident (227 columns)" and insert records from two tables with a join condition between them [incident. IN_NUM = Unit.IN_NUM].

    Is this correct, is my question? or am I mix data sources with different granularity in the same fact table. Appreciate your help.

    Best regards
    Bees

    Bees,
    Are the measures of the 'Incident', repeated during an incident given, in more than one record in the table of the unit? If so, then the sum (indicent.measure) will give an incorrect result?

    What is there to merge physically tables set outside OBIEE? With OBIEE you might have a table of 'facts' logic to present the user with report, which from tables separated units and Incidents and would stop the occurrence of incorrect aggregations. A common piece of modeling in the same way would be arrested in OBIEE headers and lines of command, quite common to have a logical fact 'orders' which contained the two header orders and order line, this translates into the Incidents-> relationship of units.

    To do what I mentioned, is relatively simple, you need a "Dim - Incident" at two levels, unit, mapp and Incident unique identifiers as keys to level and then use these levels to define the content of the levels correctly in your 2 tables logic sources logic "done", IE the LTS Incidents at incident level LTS units as level of units.

    Hope this helps, let us know if you get stuck.
    See you soon
    Alastair

Maybe you are looking for