HELP SQL (auto / full outer join with date corresponding)

I'm having a hard time get this query nailed... hoping someone can help me sorted.

create table tab1 (identification number,

date of eff_date,

Code1 varchar2 (2),

Code2 varchar2 (2)

)

/

insert into tab1 values (2, to_date('2015-01-14','YYYY-MM-DD'), 'DAT', 'AS');

insert into tab1 values (2, to_date('2015-03-19','YYYY-MM-DD'), 'DAT', 'AS');

insert into tab1 values (2, to_date('2015-08-28','YYYY-MM-DD'), 'DAT', 'AS');

insert into tab1 values (2, to_date('2015-11-12','YYYY-MM-DD'), 'DAT', 'AS');

insert into tab1 values (2, to_date('2015-01-03','YYYY-MM-DD'), "DAT", "AE");

insert into tab1 values (2, to_date('2015-03-14','YYYY-MM-DD'), "DAT", "AE");

insert into tab1 values (2, to_date('2015-04-18','YYYY-MM-DD'), "DAT", "AE");

insert into tab1 values (2, to_date('2015-09-14','YYYY-MM-DD'), "DAT", "AE");

insert into tab1 values (2, to_date('2015-01-14','YYYY-MM-DD'), "DAT", "BS");

insert into tab1 values (2, to_date('2015-02-14','YYYY-MM-DD'), "DAT", "BS");

insert into tab1 values (2, to_date('2015-03-14','YYYY-MM-DD'), "DAT", "BS");

insert into tab1 values (2, to_date('2015-05-14','YYYY-MM-DD'), 'DAT', 'BE');

insert into tab1 values (3, to_date('2015-09-16','YYYY-MM-DD'), 'DAT', 'AS');

insert into tab1 values (3, to_date('2015-04-16','YYYY-MM-DD'), "DAT", "AE");

tab1

ID, date, code 1, code2

2. DID DAT 2015-01-14

2. DID DAT 2015-03-19

2. DID DAT 2015-08-28

2. DID DAT 2015-11-12

2 AE DAT 2015-01-03

2 AE DAT 2015-03-14

2 AE DAT 2015-04-18

2 AE DAT 2015-09-14

2 BS DAT 2015-01-14

2 BS DAT 2015-02-14

2 BS DAT 2015-03-14

BE DAT 2 2015-05-14

3. DID DAT 2015-09-16

3 AE DAT 2015-04-16

What I need to do...

1 auto join to match EI for each partition ID

2. THAT the date must be less than or equal to the date of the AE and when there is more then a line corresponding to this criterion has chosen the date of closest EI of the date of the ACE.

3. it must be a full outer join because I want to show all lines, even if it is not a match.  There is a beginning, but not record end end gold but no record of departure

4. If there is an AE line for many AS lines (the SA date is less then equals the date of EI) then join this AE line to all 3 rows of ACE

5. the same rules for BS and BE.

result should look like this.

ID, date, code 1, id_1 code2, date_1, code1_1, code2_1

2 2015-01-14 DAT AS 2 AE DAT 2015-03-14

2 2015-03-19 DAT AS 2 AE DAT 2015-04-18

2 2015-08-28 DAT AS 2 AE DAT 2015-09-14

2 2015-11-12 DAT DID ZERO ZERO ZERO ZERO

NO NO NO NO 2 AE DAT 2015-01-03

2015-01-2 14 DAT BS 2 BE DAT 2015-05-14

2015-02-2 14 DAT BS 2 BE DAT 2015-05-14

2015 03-2 14 DAT BS 2 BE DAT 2015-05-14

3 2015-09-16 DAT DID ZERO ZERO ZERO ZERO

NO NO NO NO 3 AE DAT 2015-04-16

My attempt was somewhat along these lines (dealing only with SA / combos AE) but it does not manage the many scenarios one (req 4).

Select a.*, b.* from

(select row_number () on the rn (partition by a.id order a.eff_date), a.*)

of tab1 where a.code2 = 'AS') a

full outer join

(select row_number () on the rn (b.eff_date order by b.id partition), b.*)

tab1 b where b.code2 = 'Æ') b

on a.id = b.id

and a.rn = b.rn

and a.eff_date < = b.eff_date

Hello

owbdev99 wrote:

I'm having a hard time get this query nailed... hoping someone can help me sorted.

create table tab1 (identification number,

date of eff_date,

Code1 varchar2 (2),

Code2 varchar2 (2)

)

/

insert into tab1 values (2, to_date('2015-01-14','YYYY-MM-DD'), 'DAT', 'AS');

...

Thanks for posting the CREATE TABLE and INSERT.  I know it can be a lot of trouble.  You want to get answers that work, not you?  Make sure that the statements you post too much work.  Test (and, if necessary, attach) your statements before committing.  You said code1 be VARCHAR2, but all the instructions insertion have values of 3 characters for code1.

You are on the right track, with an analytical function, but ROW_NUMBER solves this problem.  1 "THAT line" could correspond to the 1st, 2nd, 3rd or any other line 'AE' and vice versa.  Try to use the analytical MIN function instead or ROW_NUMBER, like this:

WITH got_next_e_date AS

(

SELECT id, eff_date, code1, code2

MIN (CASE

WHEN SUBSTR (code2, 2) = 'E '.

THEN eff_date

END

) OVER (PARTITION BY ID.

, SUBSTR (code2, 1, 1)

ORDER BY eff_date DESC

) AS next_e_date

OF tab1

)

s AS

(

SELECT *.

OF got_next_e_date

"WHERE SUBSTR (code2, 2) s ="

)

e

(

SELECT *.

OF got_next_e_date

WHERE SUBSTR (code2, 2) = 'E '.

)

SELECT s.id

s.eff_date

s.code1

s.code2

e.id AS id_1

e.eff_date AS eff_date_1

e.code1 AS code1_1

e.code2 AS code2_1

S

FULL OUTER JOIN e ON s.id = e.id

AND s.next_e_date = e.eff_date

AND SUBSTR (s.code2, 1, 1) = SUBSTR (e.code2, 1, 1)

ORDER OF NVL (s.id, e.id)

, NVL (SUBSTR (s.code2, 1, 1)

, SUBSTR (e.code2, 1, 1)

)

s.eff_date

;

Out (as you asked):

ID EFF_DATE CODE1, CODE2 ID_1 EFF_DATE_1 CODE1_1 CODE2_1

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

2 2015-01-14 DAT AS 2 AE DAT 2015-03-14

2 2015-03-19 DAT AS 2 AE DAT 2015-04-18

2 2015-08-28 DAT AS 2 AE DAT 2015-09-14

2. DID DAT 2015-11-12

2 AE DAT 2015-01-03

2015-01-2 14 DAT BS 2 BE DAT 2015-05-14

2015-02-2 14 DAT BS 2 BE DAT 2015-05-14

2015 03-2 14 DAT BS 2 BE DAT 2015-05-14

3. DID DAT 2015-09-16

3 AE DAT 2015-04-16

I guess code2 is always 2 characters, and the 2nd character is always ' or 'E '.

I assume that the combination [id, eff_date, code2] is unique.

If these assumptions are wrong, you need a few minor changes, but nothing big.

Tags: Database

Similar Questions

  • [8i] need help with full outer join combined with a cross join...

    I can't understand how to combine a full outer join with a different type of join... is it possible?

    Here are some create table and insert for examples of database:
    CREATE TABLE     my_tab1
    (     record_id     NUMBER     NOT NULL     
    ,     workstation     VARCHAR2(4)
    ,     my_value     NUMBER
         CONSTRAINT my_tab1_pk PRIMARY KEY (record_id)
    );
    
    INSERT INTO     my_tab1
    VALUES(1,'ABCD',10);
    INSERT INTO     my_tab1
    VALUES(2,'ABCD',15);
    INSERT INTO     my_tab1
    VALUES(3,'ABCD',5);
    INSERT INTO     my_tab1
    VALUES(4,'A123',5);
    INSERT INTO     my_tab1
    VALUES(5,'A123',10);
    INSERT INTO     my_tab1
    VALUES(6,'A123',20);
    INSERT INTO     my_tab1
    VALUES(7,'????',5);
    
    
    CREATE TABLE     my_tab2
    (     workstation     VARCHAR2(4)
    ,     wkstn_name     VARCHAR2(20)
         CONSTRAINT my_tab2_pk PRIMARY KEY (workstation)
    );
    
    INSERT INTO     my_tab2
    VALUES('ABCD','WKSTN 1');
    INSERT INTO     my_tab2
    VALUES('A123','WKSTN 2');
    INSERT INTO     my_tab2
    VALUES('B456','WKSTN 3');
    
    CREATE TABLE     my_tab3
    (     my_nbr1     NUMBER
    ,     my_nbr2     NUMBER
    );
    
    INSERT INTO     my_tab3
    VALUES(1,2);
    INSERT INTO     my_tab3
    VALUES(2,3);
    INSERT INTO     my_tab3
    VALUES(3,4);
    And, the results that I want to get:
    workstation     sum(my_value)     wkstn_name     my_nbr1     my_nbr2
    ---------------------------------------------------------------
    ABCD          30          WKSTN 1          1     2
    ABCD          30          WKSTN 1          2     3
    ABCD          30          WKSTN 1          3     4
    A123          35          WKSTN 2          1     2
    A123          35          WKSTN 2          2     3
    A123          35          WKSTN 2          3     4
    B456          0          WKSTN 3          1     2
    B456          0          WKSTN 3          2     3
    B456          0          WKSTN 3          3     4
    ????          5          NULL          1     2
    ????          5          NULL          2     3
    ????          5          NULL          3     4
    I tried a number of different things, google my problem and no luck yet...
    SELECT     t1.workstation
    ,     SUM(t1.my_value)
    ,     t2.wkstn_name
    ,     t3.my_nbr1
    ,     t3.my_nbr2
    FROM     my_tab1 t1
    ,     my_tab2 t2
    ,     my_tab3 t3
    ...
    So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3. I wonder if I can't find examples of it online because it is not possible...

    Note: I'm stuck dealing with Oracle 8i

    Thank you!!

    Hello

    The query I posted yesterday is a little more complex that it should be.
    My_tab2.workstation is unique, there is no reason to make a separate subquery as mt1. We can join my_tab1 to my_tab2 and get the SUM in a subquery.

    SELECT       foj.workstation
    ,       foj.sum_my_value
    ,       foj.wkstn_name
    ,       mt3.my_nbr1
    ,       mt3.my_nbr2
    FROM       (     -- Begin in-line view foj for full outer join
              SELECT        mt1.workstation
              ,        SUM (mt1.my_value)     AS sum_my_value
              ,        mt2.wkstn_name
              FROM        my_tab1   mt1
              ,        my_tab2   mt2
              WHERE        mt1.workstation     = mt2.workstation (+)
              GROUP BY   mt1.workstation
              ,        mt2.wkstn_name
                            --
                    UNION ALL
                            --
              SELECT      workstation
              ,      0      AS sum_my_value
              ,      wkstn_name
              FROM      my_tab2
              WHERE      workstation     NOT IN (     -- Begin NOT IN sub-query
                                               SELECT      workstation
                                       FROM      my_tab1
                                       WHERE      workstation     IS NOT NULL
                                     )     -- End NOT IN sub-query
           ) foj     -- End in-line view foj for full outer join
    ,       my_tab3  mt3
    ORDER BY  foj.wkstn_name
    ,       foj.workstation
    ,       mt3.my_nbr1
    ,       mt3.my_nbr2
    ;
    

    Thanks for posting the CREATE TABLE and INSERT statements, and very clear expected results!

    user11033437 wrote:
    ... So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3.

    She, exactly!
    The trickiest part is when and how get SUM (my_value). You could address the question of exactly what my_tab3 must be attached to a cross that's exactly what should look like the result set of the full outer join between my_tab1 and my_tab2 to. To do this, take your desired results, remove columns that do not come from the outer join complete and delete duplicate rows. You will get:

    workstation     sum(my_value)     wkstn_name
    -----------     -------------   ----------
    ABCD          30          WKSTN 1
    A123          35          WKSTN 2
    B456          0          WKSTN 3
    ????          5          NULL          
    

    So the heart of the problem is how to get these results of my_tab1 and my_tab2, which is done in the subquery FOJ above.

    I tried to use auto-documenté in my code names. I hope you can understand.
    I could spend hours explaining the different parts of this query more in detail, but I don't know that I would lose some of that time, explain things that you already understand. If you want an explanation of the specific element (s), let me know.

  • Help with sql FULL OUTER JOIN

    Hi all
    I need assistance with SQL FULL OUTER JOIN.

    How to write with FULL OUTER JOIN, the query.
    I tried like


    I have 3 different queries.

    Query q: SELECT emp_id LN.emp_id.
    Sum (LN_amount) loan_amt
    MY_LON_TAB LN
    WHERE LN_amount > 20000
    LN.emp_id GROUP;



    Query b: SELECT PLN. EMP_ID emp_id,
    Sum (PLAN_AMOUNT) plan_amt
    FROM PLN MY_PLAN_TAB
    where PLAN_AMOUNT <>0
    GROUP BY PLN. EMP_ID;


    Query C:

    SELECT FLX. EMP_ID emp_id,
    SUM (PRORATE_AMOUNT) PRORATE_AMT
    OF MY_FLX_TAB FLX
    WHERE PRORATE_AMOUNT <>0
    FLX GROUP. EMP_ID;


    Suppose that the different subquery above 3 a, b, c respectively. each subquery with emp_id and respective amount.

    like a.emp_id, a.loan_amt
    b.emp_id, b.plan_amt
    c.emp_id, c.prorate_amt

    I show all the empid with their amount respective.
    schenario: If an account made, but not vice-versa b
    and B have record, but not vice versa c
    and a credit record but not vice versa c

    first: I have external is associated with the A and B
    Second: join external then complete the total outcome of (a & b) with c
    but empid c just as empty.


    My output is:

    emp_id a.loan_amt, b.plan_amt c.prorate_amt
    101 5000 null null
    102 4500 null 2000
    103 6700 null null


    How to solve the foregoing to the ANSI (FULL OUTER JOIN) method.
    Please suggest me.

    Thanks in advance...
    PKM

    Hello

    It is very difficult for anyone to say what you're doing wrong if they do not know what you are doing. Post your code FULL OUTER JOIN.
    My best guess is:

    WITH     a     AS
    (
         SELECT     ...      -- What you posted as query a
    )
    ,     b     AS
    (
         SELECT     ...      -- What you posted as query b
    )
    ,     c     AS
    (
         SELECT     ...      -- What you posted as query c
    )
    SELECT     COALESCE ( a.emp_id
               , b.emp_id
               , c.emp_id
               )     AS emp_id
    ,     ...     -- whatever other columns you want
    FROM          a
    FULL OUTER JOIN     b  ON     b.emp_id =            a.emp_id
    FULL OUTER JOIN     c  ON     c.emp_id = COALESCE (a.emp_id, b.emp_id)
    ;
    

    I hope that answers your question.
    If not, post a small example (CREATE TABLE and INSERT statements) data for all tables and the results expected from these data (if not what you have already posted).

  • Help required in a full outer join

    In my view, the query below can be changed at the full outer join. But, I was not able to do.
    I need your help to change to the full outer join. My current request is
    SELECT CLAIMNO,'1' INDX FROM D_CLAIM@CMS2PROD
    WHERE clntsys=76500 and facility=76501 and filecreatedt='18-feb-2011' and fileupdatedt is null
    MINUS
    SELECT CLAIMNO,'1' FROM D_CLAIM
    WHERE clntsys=76500 and facility=76501 and filecreatedt='18-feb-2011' and fileupdatedt is null
    UNION
    SELECT CLAIMNO,'2' FROM D_CLAIM
    WHERE clntsys=76500 and facility=76501 and filecreatedt='18-feb-2011' and fileupdatedt is null
    MINUS
    SELECT CLAIMNO,'2' FROM D_CLAIM@cms2prod
    WHERE clntsys=76500 and facility=76501 and filecreatedt='18-feb-2011' and fileupdatedt is null

    Something like:

    select nvl(p.claimno,s.claimno) claimno,
           nvl2(p.claimno,'PROD','STAGING') the_source,
           :the_clntsys clntsys,
           :the_facility facility
      from (select claimno
              from d_claim
             where clntsys = :the_clntsys
               and facility = :the_facility
           ) p
           full outer join
           (select claimno
              from d_claim@cms2prod        /* never tried with remote */
             where clntsys = :the_clntsys
               and facility = :the_facility
           ) s
           on p.claimno = s.claimno
     where p.claimno is null
        or s.claimno is null
    

    Concerning

    Etbin

  • Full outer join

    30%

    Hello

    I have a few questions to ask. Please see below I have provided a script to recreate my problem.

    Question 1.

    I have the following query does not work when I include columns (see 1.2) in the select but will run when I use the Asterix symbol (see 1.1)

    * 1.1 runs with no problems *.
    select *
      from ora full outer join txt on ora.ora_id_y = txt.txt_id_y;
    * 1.2 returns error.*
    select txt.txt_id_y
         , txt.txt_n
         , txt.txt_y
         , ora.ora_n
         , ora.ora_y
      from ora full outer join txt on ora.ora_id_y = txt.txt_id_y;
    
    Error report:
    SQL Error: ORA-00918: column ambiguously defined
    00918. 00000 -  "column ambiguously defined"
    *Cause:    
    *Action:
    Question 2.

    Is it possible to use a query with joins free and using multiple outer joins to accomplish what I have under which is seen 6 to access the query I have to Question 1.

    CREATE THE SCRIPT
    drop table master;
    
    create table master
    ( id varchar2 (10 char)
    , txt varchar2 (1 char)
    , ora varchar2 (1 char)
    );
    
    INSERT INTO MASTER (ID, TXT, ORA) VALUES ('orange' , 'Y', 'Y');
    INSERT INTO MASTER (ID, TXT, ORA) VALUES ('apple', 'Y', '');
    INSERT INTO MASTER (ID, TXT, ORA) VALUES ('orange'  , 'Y', 'N');
    INSERT INTO MASTER (ID, TXT, ORA) VALUES ('peach'  , 'Y', '');
    
    create or replace force view ora_n
    as
    select id
         , count (ora) ORA_N
      from master
     where ora in ('N')
     group by id;
    
    create or replace force view ora_y
    as
     select id
         , count (ora) ORA_Y
      from master
     where ora in ('Y')
     group by id;
    
    create or replace force view txt_n
    as 
     select id
         , count (txt) TXT_N
      from master
     where txt in ('N')
     group by id;
    
    create or replace force view txt_y
    as 
     select id
           , count (txt) TXT_Y
      from master
     where txt in ('Y')
     group by id;
     
    create or replace force view ora
    as 
    select ora_n.id ora_id_n
         , ora_y.id ora_id_y
         , ora_n.ora_n
         , ora_y.ora_y
      from ora_n full outer join ora_y on ora_n.id = ora_y.id;
    
    create or replace force view txt
    as 
    select txt_y.id txt_id_n
         , txt_y.id txt_id_y
         , txt_n.txt_n
         , txt_y.txt_y
      from txt_n full outer join txt_y on txt_n.id = txt_y.id; 
    Published by: benton on August 21, 2012 10:48

    Published by: benton on August 21, 2012 11:11

    Found: support for Oracle, there are:

    Bug 6319169 : ORA-918, FULL OUTER JOIN
    Reproduced on 10.2.0.1,10.2.0.3.
    Fixed to the product Version 11.0
    WORKAROUND solution: alter session set '_column_elimination_off' = true;

  • What is the Assembly of 4 or 5 tables FULL OUTER JOIN logic?

    the query is as below:

    as you can see, I need FULL OUTER JOIN, these 5 tables and get a column from each table that is based on three common columns (ORG_iD, CUST_ID, CURRENT_DT).

    And I wonder what is the logic of the 5 FULL OUTER JOIN table?
    It will return if there is a folder that exists in the single table COL1? or table, COL2 and COL3 and so on. Basically any combination of these 5 tables.

        SELECT (CASE WHEN COL1.ORG_ID IS NOT NULL THEN COL1.ORG_ID
                     WHEN COL2.ORG_ID IS NOT NULL THEN COL2.ORG_ID
                     WHEN COL3.ORG_ID IS NOT NULL THEN COL3.ORG_ID
                     WHEN COL4.ORG_ID IS NOT NULL THEN COL4.ORG_ID
                     ELSE COL5.ORG_ID 
                END) ORG_ID,
                (CASE WHEN COL1.CUST_ID IS NOT NULL THEN COL1.CUST_ID
                     WHEN COL2.CUST_ID IS NOT NULL THEN COL2.CUST_ID
                     WHEN COL3.CUST_ID IS NOT NULL THEN COL3.CUST_ID
                     WHEN COL4.CUST_ID IS NOT NULL THEN COL4.CUST_ID
                     ELSE COL5.CUST_ID
                END) CUST_ID, 
                (CASE WHEN COL1.CURRENT_DT IS NOT NULL THEN COL1.CURRENT_DT
                     WHEN COL2.CURRENT_DT IS NOT NULL THEN COL2.CURRENT_DT
                     WHEN COL3.CURRENT_DT IS NOT NULL THEN COL3.CURRENT_DT
                     WHEN COL4.CURRENT_DT IS NOT NULL THEN COL4.CURRENT_DT
                     ELSE COL5.CURRENT_DT 
                END) CURRENT_DT, 
              'VENDORS' as ITEM_NAME,
              V_AGE_IND as ACCT_AGE_IND,  
              COL1.AMT_ZSHT,  
              COL2.AMT_YFYS,  
              COL3.AMT_YFYS, 
              COL4.AMT_YFYS,  
              COL5.AMT_BZL,  
              'NAV',
              sysdate
        FROM COL1  
                  FULL OUTER JOIN COL2 ON (COL1.ORG_ID=COL2.ORG_ID AND COL1.CURRENT_DT=COL2.CURRENT_DT AND COL1.CUST_ID=COL2.CUST_ID)  
                  FULL OUTER JOIN COL3 ON (COL2.ORG_ID=COL3.ORG_ID AND COL2.CURRENT_DT=COL3.CURRENT_DT AND COL2.CUST_ID=COL3.CUST_ID)  
                  FULL OUTER JOIN COL4 ON (COL3.ORG_ID=COL4.ORG_ID AND COL3.CURRENT_DT=COL4.CURRENT_DT AND COL3.CUST_ID=COL4.CUST_ID)  
                  FULL OUTER JOIN COL5 ON (COL4.ORG_ID=COL5.ORG_ID AND COL4.CURRENT_DT=COL5.CURRENT_DT AND COL4.CUST_ID=COL5.CUST_ID)  
    Any ideas?

    Thank you

    Hello

    When you perform a JOIN EXTERNAL COMPLETE multi - build that way, whenever you add a new table, you can just join the previous table because this table could be all NULL values for a given row. You can reach each new table to the first table, either, for the same reason. You must enclose each new table with all previous tables, like this:

    FULL OUTER JOIN COL2 ON  COL2.ORG_ID     =           COL1.ORG_ID
                   AND COL2.CURRENT_DT =           COL1.CURRENT_DT
                   AND COL2.CUST_ID    =           COL1.CUST_ID
    FULL OUTER JOIN COL3 ON  COL3.ORG_ID     = COLAESCE (COL1.ORG_ID,     COL2.ORG_ID)
                   AND COL3.CURRENT_DT = COALESCE (COL1.CURRENT_DT, COL2.CURRENT_DT)
                   AND COL3.CUST_ID    = COALESCE (COL1.CUST_ID,    COL2.CUST_ID)
    FULL OUTER JOIN COL3 ON  COL4.ORG_ID     = COLAESCE (COL1.ORG_ID,     COL2.ORG_ID,     COL3.ORG_ID)
                   AND COL4.CURRENT_DT = COALESCE (COL1.CURRENT_DT, COL2.CURRENT_DT, COL3.CURRENT_DT)
                   AND COL4.CUST_ID    = COALESCE (COL1.CUST_ID,    COL2.CUST_ID,    COL3.CUST_ID)
    FULL OUTER JOIN COL3 ON  COL5.ORG_ID     = COLAESCE (COL1.ORG_ID,     COL2.ORG_ID,     COL3.ORG_ID,     COL4.ORG_ID)
                   AND COL5.CURRENT_DT = COALESCE (COL1.CURRENT_DT, COL2.CURRENT_DT, COL3.CURRENT_DT, COL4.CURRENT_DT)
                   AND COL5.CUST_ID    = COALESCE (COL1.CUST_ID,    COL2.CUST_ID,    COL3.CUST_ID,    COL4.CUST_ID)  
    

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements), and the results you want from this data.
    Explain how you get these results from these data.
    Always tell what version of Oracle you are using.

  • Left Outer Join with sum

    Dear elders,



    Firstly, sorry if my question is considered to be 'very novice' but I have this problem of confusion.

    Can I use the left outer join with summary?
    For example:

    Work table
    JobCode      JobGroupCode      
    111A         1100              
    112B         1100               
    113C         1100
    121A         1200
    333F         3300               
    Table No.
    JobGroupCode    ParentCode
    1100            1000
    1200            1000
    1300            1000
    3300            3000
    Activity table
    Jobcode      Mandays
    111A         5
    112B         7
    113C         3
    I want to choose with this result
    Job.JobCode    Job.JobGroupCode     JobGroup.Parentcode       Mandays
    111A           1100                 1000                      5
    112B           1100                 1000                      7               
    113C           1100                 1000                      3
    121A           1200                 1000                      0
    333F           3300                 3000                      0
    All I did was:
    select j.jobcode, j.jobgroupcode, jg.parentcode, sum(a.mandays)
      from job j
     inner join jobgroup jg on j.jobgroupcode = jg.jobgroupcode
      left join activity a on j.jobcode = a.jobcode
     group by j.jobcode, j.jobgroupcode, jg.parentcode
    and I got was only jobcode activity table, not exactly what I want to get all the work table jobcode. Could you please tell me, where I was wrong?

    result
    Job.JobCode    Job.JobGroupCode     JobGroup.Parentcode       Mandays
    111A           1100                 1000                      5
    112B           1100                 1000                      7               
    113C           1100                 1000                      3
    Thank you very much.

    Published by: user11197113 on May 25, 2009 03:31

    Published by: user11197113 on May 25, 2009 03:32

    Hello (and welcome!).

    Edit

    OK, try this:

    select j.jobcode, j.jobgroupcode, jg.parentcode, NVL(sum(a.mandays),0)
      from job j
     inner join jobgroup jg on j.jobgroupcode = jg.jobgroupcode
      left join activity a on j.jobcode = a.jobcode
     group by j.jobcode, j.jobgroupcode, jg.parentcode
    

    That will give you this:

    JOBC JOBG PARE SUM(A.MANDAYS)
    ---- ---- ---- --------------
    111A 1100 1000              5
    112B 1100 1000              7
    113C 1100 1000              3
    121A 1200 1000              0
    333F 3300 3000              0
    

    These are real results of your test data.

  • full outer join: how to get without null values?

    full outer join: how to get without null values?

    Hello

    Please go well this url

    http://asktom.Oracle.com/pls/asktom/f?p=100:11:999478429860455:P11_QUESTION_ID:6585774577187

    Thank you
    Prakash P

  • Diff "full outer join" is going. "(+)" SELECT syntax?

    As far as I know there are two ways to set an outer join:

    Select... from tab1 external t1 full join tab2 t2 on t1.id = t2.id;
    or:
    SELECT... from tab1 tab2 t2, t1 where t1.id = t2.id (+)

    They are absolutely equivialent?

    Who are the most common one?

    Who serve as well on other databases (DB2, MySQL)?

    Can I put an additional WHERE clause at the end of the first statement:

    SELECT... from tab1 t1 outer join t2 on t1.id = t2.id tab2 complete WHOSE...;

    Select... from tab1 external t1 full join tab2 t2 on t1.id = t2.id;

    It's FULL OUTER JOIN

    SELECT... from tab1 tab2 t2, t1 where t1.id = t2.id (+)

    It's LEFT OUTER JOIN.

    They are quite different.

    FULL OUTER JOIN - retrieves all the rows from tab1 and tab2

    LEFT OUTER JOIN - gets all the tab1 and lines than the line that matches the join condition of tab2.

    Here is a simple test.

    create table tab1(id integer)
    /
    create table tab2(id integer)
    /
    begin
      insert into tab1 values(1);
      insert into tab1 values(2);
      insert into tab1 values(3);
    end;
    /
    begin
      insert into tab2 values(3);
      insert into tab2 values(4);
      insert into tab2 values(5);
    end;
    /
    Select t1.id, t2.id
      from tab1 t1 full outer join tab2 t2
        on t1.id =t2.id
    /
    Select t1.id, t2.id
      from tab1 t1, tab2 t2
     where t1.id = t2.id(+)
    /
    

    Run it and see the result.

  • SSRS for lack of outer join with the Oracle data source

    It seems to be a problem with the Oracle driver used in the Reporting SERVICES query designer.

    When you use an Oracle data source, if I create an outer join in the graphic designer, it automatically inserts '{OJ' before the join and '} ' after her.  This is an incorrect syntax for Oracle and refuses to start.  The curly braces and the JO editable in designer text, but if I go back to the graphic designer and immediately to reintegrate them.

    Only, this has started to happen a year or two ago - before that it worked, but with the old (+) syntax.

    Can it not be healed?  It makes things very difficult.

    -Geoff

    Hi Geoff,

    Thanks for posting in the Microsoft Community.

    However, the question you posted would be better suited in the Forums of the Oracle Support; We recommend that you post your query in Oracle Support Forums to get help:

    https://forums.Oracle.com/forums/main.jspa;JSESSIONID=8d92100c30d8fb401bcbd10b46c38c9ddf1a3242549a.e34SbxmSbNyKai0Lc3mPbhmSc3aNe0? CategoryID = 84

    If you have any other questions or you need Windows guru, do not hesitate to post your questions and we will be happy to help you.

  • Outer join with a constant

    Outer join when made between the columns of the two tables is easy to understand and I'm familiar with it. But when it comes to the same join with a constant, my head starts spinning... you guys can help me with this? I would appreciate that show you with examples.
    Thanks a ton.

    Hello

    >
    Ok... I want to understand the output of the last sql statement given here.
    ...
    Select * from a, b
    where col_a (+) = col_b
    and col_a (+) = 1

    It shows put it is as follows
    col_a, col_b
    1 1
    2 NULL
    NULL NULL

    Joining means: 'display all rows in table b and the corresponding rows in the table one '.
    Since it is an outer join, it also means "view b lines even if there is no corresponding row in a.

    Thus, the output contains 3 lines, which correspond to the 3 lines in b.
    Only one of them (the one with col_b = 1) had a game in a.col_a, if the column is NULL in the other lines.

    With the sample data you posted, you will get the same results without condition

    and     col_a (+) = 1
    

    If you want to understand what makes this condition, use some examples of data where this condition would fail.
    For example, add this line to the table a:

    insert into a values (2);
    

    and run your original query (including the)

    and     col_a (+) = 1
    

    ). what output you get? You get the same exact results you did without the new line in a.

    In other words, the b line where col_b = 2 has still no matches in the table has.
    True, there is now a line of table where col_a = 2, but which does not constitute a game. A match is defined by two conditions:

    where   col_a (+) = col_b
    and     col_a (+) = 1
    

    in order to have a pair of lines which meets only the first condition

    where   col_a (+) = col_b
    

    is not enough.

  • Help for a LEFT OUTER JOIN query

    Hello, all,.

    I'm having some trouble setting up an Oracle 11 g Server SQL query, and I could use some help.

    Let's say tableA is blogs; tableC is comments for blog entries; tableB is the associative array:

    tableA
    blogID        blogTitle       blogBody      dateEntered
    1             This is a test  More text...  2016-05-20 11:11:11
    2             More testing    Still more!   2016-05-19 10:10:10
    3             Third charm!!   Blah, blah.   2016-05-18 09:09:09
    

    tableC
    commID        userID          userText      dateEntered
    10            Bravo           I like it!    2016-05-20 11:21:31
    11            Charlie         I don't!      2016-05-20 11:31:51
    12            Alpha           Do it again!  2016-05-19 10:20:30
    13            Bravo           Still more?   2016-05-19 10:30:50
    14            Charlie         So, what?     2016-05-19 10:35:45
    15            Bravo           Blah, what?   2016-05-18 09:10:11
    16            Alpha           Magic number! 2016-05-18 09:11:13
    

    tableB
    blogID        commID
    1             10
    1             11
    1             12
    2             13
    2             14
    3             15
    3             16
    
    
    
    

    I'm trying to get blogID, blogTitle, blogBody and the number of comments for each blog entry.  But, since I'm on to_char() for date and COUNT (commID) for the total number of comments, I am not "a group by expression.

    Here is an example of pseudo-SQL of what I'm trying.

    SELECT a.blogID, a.blogTitle, a.blogBody, to_char(a.dateEntered,'YYYY-MM-DD HH24:MI:SS') as dateEntered, COUNT(c.commID) as total
    FROM tableA a LEFT OUTER JOIN tableB b ON b.blog_ID = a.blog_ID
                  LEFT OUTER JOIN tableC c ON c.commID = b.commID
    WHERE a.blogID = '1'
    GROUP BY blogID, blogTitle, blogBody
    ORDER BY to_date(dateEntered,'MM-DD-YYYY HH24:MI:SS') desc
    

    I'm sure it's something simple, but I just DO NOT see it.  Can you help me?

    V/r,

    ^_^

    Try:

    GROUP BY a.blogID, a.blogTitle, a.blogBody, to_char(a.dateEntered,'YYYY-MM-DD HH24:MI:SS')
    

    See you soon

    Eddie

  • outer join with the additional constraint

    Hello

    With the help of Oracle 11 g R2.

    I would of outer join tables 2 together and put down restrictions on the types of records that are returned in the query result. Here's a mock-up of the tables and data.

    create table aaa (col1 number not null, col2 number not null)

    create table bbb (col1 number not null, col2 number not null)

    insert into values of aaa (1: 80)

    insert into values aaa (2, 90)

    insert into values aaa (3, 80)

    insert into values aaa (4, 90)

    insert into values aaa (5, 80)


    insert into bbb values (3, 600)

    insert into values of bbb (4, 700)

    This is the query

    
    select a.col1, a.col2, b.col1, b.col2
    from aaa a, bbb b
    where a.col1 = b.col1 (+)
    and   (a.col2, b.col2) <> ((90, 700))
    

    The result of the query is as follows.

    col1 col1 col2 col2

    1 80

    3 80 3 600

    5 80

    Where col1 = 4 has been deleted, which is an expected result. However, where col1 = 2 has also been removed, which is not a desired outcome. Your response is appreciated.

    Hello

    Here is a way that works for the given sample data:

    SELECT *.

    AAA a

    LEFT OUTER JOIN bbb b ON a.col1 = b.col1

    WHERE the NVL (a.col2, 0) <> 90

    OR NVL (b.col2, 0) <> 700

    ;

    I don't know if that will satisfy your requirements with other data, since you didn't say what your needs are.

    Whenever you have a WHERE clause is applied after the outer join, all columns of the table in option (table bbb in this example) must be used in an NVL, NVL2 or something like a CASE expression that takes into account null values; otherwise, the effect will be the same as an inner join.

  • Left outer join without data in a table

    I have two tables defined (see below). Emp table has given, and there is still no data in the table of Emp_Type! Now it is empty.

    I want to write a query that returns the data from the tables, even if there is no data in the Emp_type table. I use a left outer join but it return nothing. Can anyone help?
    create table EMP
    (
      EMPID   NUMBER(10,2),
      EMPNAME VARCHAR2(100)
    );
    
    INSERT INTO emp (empid,empname) values (1, 'Mark');
    INSERT INTO emp (empid,empname) values (2, 'Jason');
    INSERT INTO emp (empid,empname) values (3, 'Kevin');
    INSERT INTO emp (empid,empname) values (4, 'Drew');
    INSERT INTO emp (empid,empname) values (5, 'Jessica');
    INSERT INTO emp (empid,empname) values (6, 'Pena');
    INSERT INTO emp (empid,empname) values (7, 'Roxanne');
    
    create table EMP_Type
    (
      ID          NUMBER(10,2),
      EMPID       NUMBER(10,2),
      TYPE_ID     NUMBER(10,2),
      END_DATE    DATE
    );
    
    No data
    
    select *
      from emp e
      left outer join emp_Type t
        on e.empid = t.empid
     WHERE t.type_id = 1
       and t.end_date is null;

    WHERE t.type_id = 1

    will be ever true when table has no rows (or t.type_id is NULL?)

  • Outer join with Oracle syntax

    Hi, I am new to oracle and I worked this request for reports for about 2 weeks, please take a look at my request
    SELECT mmt.transaction_date "Transaction Date", 
                msib.segment1 "Item", 
                gcc.segment1||'-'||gcc.segment2||'-'||gcc.segment3||'-'||gcc.segment4||'-'||gcc.segment5||'-'||gcc.segment6||'-'||(nvl(gcc.segment7,'000000')) "account",
                (CASE mttype.transaction_type_name
                WHEN 'Average cost update' THEN
              ((nvl(mmt.new_cost,0) - nvl(mmt.prior_cost,0)) * nvl(mmt.quantity_adjusted,0)) + nvl(mmt.variance_amount,0)
                ELSE
                 (mmt.Primary_quantity * nvl(mmt.actual_cost, 0) + nvl(mmt.variance_amount, 0))
               END) "Transaction Value",
                mttype.description "Transaction Type",
                mmt.subinventory_code "Subinventory",
                ood.organization_code "Org",
                msib.Primary_UOM_Code "UOM",
                mmt.Primary_Quantity "Primary Quantity",
                mtr.description "Reason",
                mmt.transaction_reference "Reference"
    FROM mtl_material_transactions mmt,
             mtl_system_items_b msib,
             mtl_transaction_accounts mta,
             gl_code_combinations gcc,
             mtl_transaction_types mttype,
             Org_Organization_Definitions ood,
             mtl_transaction_reasons mtr
    WHERE mmt.transaction_date >= :P_DATE_FROM 
               and mmt.transaction_date  < :P_DATE_TO +1
               and mmt.organization_id = :P_ORGANIZATION 
               and msib.organization_ID = mmt.organization_ID  
               and msib.inventory_item_id=mmt.inventory_item_id 
               and mta.transaction_id=mmt.transaction_id
               and gcc.code_combination_id = mta.reference_account
               and mttype.transaction_type_id=mmt.transaction_type_id
               and mmt.reason_id=mtr.reason_id(+)
               and ood.organization_id=mmt.organization_id
               and mttype.transaction_type_id = :P_TRANSACTION_TYPE
               and msib.segment1 = :P_ITEM
               AND gcc.segment2 = :P_ACCOUNT
    null values is on the mtl_material_transactions table, which is reason_id, subinventory_code and transaction_reference
    put desired option on would show all archives on mtl_material_transactions with null values
    so I put the symbol of the outer join right?
    BTW
    I tried to put the (+) sign on various locations but it still not good and I am really at a loss

    Hello

    Whenever you have any questions, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and the desired results from these data. I know that's not always easy, but it is really necessary. Compounds that, I do not understand your problem.
    It could be useful that simplify you the problem. If you were not interested in, say, the mta, gcc, tables msib and ood, would you still have the same problem? If so, forget all these tables and just after, CREATE TABLE and INSERT statements for the remaining tables and the results you want from this data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

    The query you posted will not exclude any line of mmt just because it doesn't have a corresponding line in the mtr; That's what the condition:

    and mmt.reason_id=mtr.reason_id(+)
    

    but it will exclude mmt lines if they do not have matching rows in other tables. Maybe you need + plus join and may under certain conditions non-join, such as conditions

    gcc.segment2 (+) = :P_ACCOUNT
    

    Too much. It's just a guess. Without seeing your sample data and the correct results, you should get from this data, I can't say.

Maybe you are looking for

  • Cannot send SMS on iOS 10

    Hello world So I updated my iPhone 6 Plus and my iPad Pro 12.9 "IOS 10 and since then I can not send TEXT messages from my iPad. I receive them properly but just cannot send their right. iMessages but just seem to work well. Any suggestions? Thank yo

  • Question about driver ATI for Satellite A60

    I try to scan my PC: Toshiba Satellite A60 the driveragent.com and the result is:http://DriverAgent.com/driveragent_results.php?HWID=3398ca2cff2f976c63feb c0403dcb86e & n = 8562751 & PHPSESSID = 996c598e28ed091353 506e834b351b34 Unfortunately, I can'

  • Satellite Pro L450 - 17K - expandability RAM and HARD drive

    Can someone help me find out what the actual amount of system ram memory can be installed using windows 7 64 bit? As far as I can tell it is 4 GB or more? What is "more"? What of the hard disk, what is the maximum size that I am able to install and f

  • BlackBerry smartphones can not access e-mail, internet or BBM

    Hi, I think I have a problem similar to the previous posters... my mother gave me her blackberry tour when it improved and I had unlocked it (from a cell in Sasktachewan provider) so that I can use it for my move to Dubai.  In Canada, I was able to u

  • BlackBerry tour Smartphones Enterprise activation

    On a new tour from Verizon yesterday, bought Options > Advanced Options > Enterprise Activation is missing. Download the User Guide and getting started Guide, and there is no reference to 'The Enterprise Activation' moved. Any ideas on what menu, thi