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).

Tags: Database

Similar Questions

  • 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.

  • [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 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.

  • Need help with Sql Tunning

    Under Update taking 2 h complete 3000 sets.

    UPDATE ARR_TRANSACTIONS ARR

    SET = NUMBER99

    (SELECT ARR_1.NUMBER99

    OF ARR_TRANSACTIONS ARR_1, ARR_HEADER BH

    WHERE ARR_1.ARR_ID = BH. ARR_ID

    AND BH. FLAG = "Y".

    AND ARR_1.LINE_ID = ARR. ORIG_INV_LINE_ID

    AND ARR_1.NUMBER9 IS NOT NULL

    AND ROWNUM = 1)

    WHERE ARR BATCH_ID = 26219

    AND ARR. NUMBER99 IS NULL

    I added more some condition where to reduce cost and its working fine. But I need to rise more than performance on SQL.

    Please help with SQL.

    I do not see the number of 3000 lines in the plan - so I guess that the optimizer does not compute with the correct numbers. Could generate the plan with dbms_xplan.display_cursor? This could tell us something about filter predicates and access.

    In the given situation, I would check:

    • is the access to the index in step 1 (filtered by the relevant conditions: this could be BATCH_ID = 26219) return actually 248 entries (or something similar)?
    • Access table in step 2 returning 204 lines (or something similar) after applying the filters given? If the number of lines is much bigger then a Nested Loops is perhaps not a good idea.

    To make this much simpler check, you could create a plan with statistics of content (RowSource) given that the plan containing the real and the number of estimated rows.

    If the estimated cardinalities are not plausible the next question would be if the statistics are strong and up to date.

  • 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.

  • Need help with sql query performance

    Dear all,

    I have a sql like query below, I need to give the following query please help me identify which statement I should tune to have better performanece.

    Select rownum LINE_NUM,

    A.LINE_ID,

    TO_CHAR (A.INVITMID),

    TO_NUMBER (A.PICKQTY),

    UNLOADINGPNT NULL,

    RRNUM NULL,

    WORKORDNUM NULL,

    WORKORDDESC NULL,

    A.PONUM,

    DTR_DUMB NULL,

    A.DESCRIPTION,

    FROM_SUB NULL,

    TO_SUB NULL,

    NO SOURCE,

    ASSET_NUMBER NULL,

    A.RECEIPTNUM,

    MOVEORD NULL,

    FROM_LOC NULL,

    TO_LOC NULL,

    MSD_NUM NULL,

    CONTAIN_LINE NULL,

    A.UOM,

    A.PO_RELEASE

    de)

    Select headerid Po.Po_Header_Id,

    rcv1. Po_Line_Id LINE_ID,

    rcv1.item_id INVITMID,

    (NVL(Rcv1.Transact_Qty,0)-NVL(rcv2.transact_qty,0)) PICKQTY,

    Po.Segment1 PONUM,

    Rcv1.Receipt_Num RECEIPTNUM,

    Rcv1.Item_Desc DESCRIPTION,

    Rcv1.Transact_Uom GLU,

    Rcv1.Po_release

    Po_Headers_All in.,.

    (Select rcv3. Po_Header_Id, RCV3.receipt_num, rcv3. Po_Line_Id, rcv3. Destination_Type_Code, rcv3. Item_Id, rcv3. Item_Desc, rcv3. Transact_Uom, SUM (rcv3. Transact_Qty) Transact_Qty, rcv3. PO_RELEASE OF)

    SELECT A.Po_Header_Id,

    C.RECEIPT_NUM receipt_num,

    A.Po_Line_Id,

    A.Destination_Type_Code,

    B.Item_Id,

    B.item_description Item_Desc,

    A.UNIT_OF_MEASURE Transact_Uom,

    A.QUANTITY Transact_Qty,

    D.RELEASE_NUM PO_RELEASE

    OF RCV_TRANSACTIONS,.

    RCV_SHIPMENT_HEADERS C.

    B RCV_SHIPMENT_LINES,

    PO_RELEASES_ALL D

    WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID

    AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID

    AND UPPER (A.Transaction_Type) = "to DELIVER".

    AND higher (A.Destination_Type_Code) = "EXPENSES".

    AND D.PO_RELEASE_ID = A.PO_RELEASE_ID

    UNION ALL

    SELECT A.Po_Header_Id,

    C.RECEIPT_NUM receipt_num,

    A.Po_Line_Id,

    A.Destination_Type_Code,

    B.Item_Id,

    B.item_description Item_Desc,

    A.UNIT_OF_MEASURE Transact_Uom,

    A.QUANTITY Transact_Qty,

    D.RELEASE_NUM PO_RELEASE

    OF RCV_TRANSACTIONS,.

    RCV_SHIPMENT_HEADERS C.

    B RCV_SHIPMENT_LINES,

    PO_RELEASES_ALL D

    WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID

    AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID

    AND B.ITEM_ID IS NULL

    AND UPPER (A.Transaction_Type) = "to DELIVER".

    AND higher (A.Destination_Type_Code) = "WORKSHOP".

    D.PO_RELEASE_ID AND = A.PO_RELEASE_ID) rcv3

    GROUP BY rcv3. Po_Header_Id, RCV3.receipt_num, rcv3. Po_Line_Id, rcv3. Destination_Type_Code, rcv3. Item_Id, rcv3. Item_Desc, rcv3. Transact_Uom, rcv3. Rcv1 PO_RELEASE),

    (SELECT A.PO_LINE_ID,

    Sum (A.Quantity) transact_qty,

    A.PO_HEADER_ID,

    C.RECEIPT_NUM

    OF RCV_TRANSACTIONS,.

    RCV_SHIPMENT_HEADERS C.

    B RCV_SHIPMENT_LINES,

    PO_RELEASES_ALL D

    WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID

    AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID

    AND UPPER (A.Transaction_Type) = "RETURN to the RECEPTION"

    AND D.PO_RELEASE_ID = A.PO_RELEASE_ID

    A.PO_LINE_ID, A.PO_HEADER_ID, C.RECEIPT_NUM GROUP) Rcv2

    Where Po.Po_Header_Id = Rcv1.Po_Header_Id (+)

    And Rcv1.Po_Line_Id = Rcv2.Po_Line_Id (+)

    And Rcv1.Receipt_Num = Rcv2.Receipt_Num (+)

    And Rcv1.Transact_Qty <>Nvl(Rcv2.Transact_Qty,999999999)

    Group of po.po_header_id, rcv1.po_line_id, po.segment1, rcv1.receipt_num, rcv1.item_id, Rcv1.Item_Desc, rcv1. TRANSACT_UOM, rcv1. PO_RELEASE, (NVL(Rcv1.Transact_Qty,0)-NVL(RCV2.transact_qty,0))) has

    Is my version of the database: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    Please find the attached PLAN to EXPLAIN.

    EXPLAINPLAN.jpg

    Thank you

    May be essentially the same join (between a, b, c and d) twice instead of three times

    sounds like it could be done with a single join (between a, b, c, and d), but you do not some columns than the columns of rcv1 rcv2

    We can work on what we can see only

    Select rownum line_num,

    rcv1.po_line_id line_id,

    TO_CHAR (rcv1.item_id) invitmid,

    NVL(rcv1.transact_qty,0) - nvl(rcv2.transact_qty,0) pickqty,

    unloadingpnt null,

    rrnum null,

    workordnum null,

    workorddesc null,

    Po. Ponum Segment1,

    dtr_dumb null,

    description of the rcv1.item_desc,

    from_sub null,

    to_sub null,

    No source,

    asset_number null,

    rcv1.receipt_num receiptnum,

    moveord null,

    from_loc null,

    to_loc null,

    msd_num null,

    contain_line null,

    Glu rcv1.transact_uom,

    rcv1.po_release

    of po_headers_all in.

    left outer join

    (select a.po_header_id,

    c.receipt_num,

    a.po_line_id,

    a.destination_type_code,

    b.item_id,

    b.item_description item_desc,

    a.unit_of_measure transact_uom,

    Sum (a.Quantity) transact_qty,

    d.release_num po_release

    from (select shipment_header_id,

    shipment_line_id,

    po_release_id,

    po_header_id,

    po_line_id,

    destination_type_code,

    unit_of_measure,

    quantity

    of rcv_transactions

    where upper (a.transaction_type) = "to DELIVER".

    and upper (a.destination_type_code) ('charge', 'WORKSHOP')

    ) a

    inner join

    rcv_shipment_lines b

    On a.shipment_line_id = b.shipment_line_id

    inner join

    c rcv_shipment_headers

    On a.shipment_header_id = c.shipment_header_id

    left outer join

    po_releases_all d

    On a.po_release_id = d.po_release_id

    where upper (a.destination_type_code) = "EXPENSES".

    or (upper (a.destination_type_code) = 'WORKSHOP'

    and b.item_id is null

    )

    A.po_header_id group,

    c.receipt_num,

    a.po_line_id,

    a.destination_type_code,

    b.item_id,

    b.item_description,

    a.unit_of_measure,

    d.release_num

    ) rcv1

    On po.po_header_id = rcv1.po_header_id

    left outer join

    (select a.po_line_id,

    Sum (a.Quantity) transact_qty,

    a.po_header_id,

    c.receipt_num

    from (select shipment_header_id,

    shipment_line_id,

    po_release_id,

    po_header_id,

    quantity

    of rcv_transactions

    where upper (a.transaction_type) = "RETURN to THE RECIPIENTS.

    ) a

    inner join

    rcv_shipment_lines b

    On a.shipment_line_id = b.shipment_line_id

    inner join

    c rcv_shipment_headers

    On a.shipment_header_id = c.shipment_header_id

    left outer join

    po_releases_all d

    On a.po_release_id = d.po_release_id

    A.po_line_id group,

    a.po_header_id,

    c.receipt_num

    ) rcv2

    On rcv1.po_line_id = rcv2.po_line_id

    and rcv1.receipt_num = rcv2.receipt_num

    where rcv1.transact_qty! = nvl(rcv2.transact_qty,999999999)

    Concerning

    Etbin

  • 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

  • Where clause ' where 1 = 1 ' help with SQL tuning

    Hello Tuning specialists,.
    It is useful to use ' where 1 = 1 ' and then type all joins and conditions in the statements 'AND' of the SQL when writing SQL queries. I would like to know if it helps with the writing queirs this way SQL query performance.

    Thanks in advance.

    Published by: oracle_developer on October 8, 2012 10:41

    oracle_developer wrote:
    Hello Tuning specialists,.
    It is useful to use ' where 1 = 1 ' and then type all joins and joins in the statements "AND" when writing SQL queries.
    I would like to know if it helps with the writing queirs this way SQL query performance.

    He has No. performance impact.
    It is done when building dynamic SQL, so each sentence can start with AND

  • Need help with SQL/PL/SQL for Dates

    Hi Experts - need help with a SQL query.

    I need to insert some date fields in a table called CALENDAR_PERIOD.

    Current data in CALENDAR_PERIOD table with their data types:

    STARTPERIOD (DATE) YEAR (NUMBER) PERIOD_LABEL (Varchar2 255)

    02/11/2014 2014 2014/02/11 SUN

    03/11/2014 2014 14/03/11 MON

    04/11/2014 2014 11/04/14 MAR

    I have to increment above values up to the year 2025. I don't know how to write SQL and increment of these values.

    Ex: My next value should insert: 05/11/2015 2014 11/05/14 WED like that I need to insert data until 12 31, 2025.

    Can you please help me with PL/SQL block?

    Really appreciate your help!

    DB version:

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    PL/SQL Release 11.2.0.3.0 - Production

    CORE Production 11.2.0.3.0

    AMT for IBM/AIX RISC System/6000: Version 11.2.0.3.0 - Production

    NLSRTL Version 11.2.0.3.0 - Production

    Thank you
    Sandy

    Hello Sandy,

    Maybe something like

    INSERT INTO calendar_period (startperiod, year, period_label)

    SELECT DATE '' 2014-11-04 + LEVEL

    , TO_NUMBER (TO_CHAR (DATE '' 2014-11-04 + LEVEL, "YYYY"))

    , TO_CHAR (DATE '' 2014-11-04 + LEVEL, "MM/DD/YY DY")

    OF the double

    CONNECT BY LEVEL<= date="" '2025-12-314="" -="" date="">

    ;

    ((mais je ne comprends pas pourquoi nous créons une telle table "année" et "period_label" peuvent être calculé à partir de startperiod))

    Best regards

    Bruno Vroman.

  • Help with SQL Query (Subselects)

    Hello community,

    IAM a new Member in this forum. The first excuse my English, my native language is German.
    In my workplace, we have a great Orcle Database 11 g with 30 different tables for production control issues.
    I try to get a couple of different information from the database, so I started with SQL of the query, but for this problem, I wasn't able to write a query to work.

    In this case, I have 2 tables:
    Table 1:
    ID; ORDER_NR; DESCRIPTION; CREATE_DATE
    1; A500236; CLEAN HOUSE; 20/02/2012
    2; A623555; REPAIR CAR; 10/01/2012
    3; A866944; MAINTAIN EQUIPMENT; 11/02/2012

    Table 2:
    ID; ORDER_NR; WO_STEP; STEP_DATE; EMPLOYEE
    1; A500236; A; 21/02/2012; W0010
    2; A500239; F; 21/02/2012; W0010
    3; A500239; S; 22/02/2012; W0027
    4; A500239; R; 23/02/2012; H0034
    5; A500239; U; 25/02/2012; L0099
    6; A263555; A; 15/01/2012; G0009
    7; A263555; C; 17/01/2012; S0039
    5 V A263555; R; 18/01/2012; K0059
    9; A263555; U; 19/01/2012; A0048
    10; A866944; A; 13/02/2012; H0034
    11; A866944; B; 13/02/2012; L0035
    12; A866944; G; 17/02/2012; D0084
    13; A866944; U; 23/02/2012; S0039

    And the result of my query should look like this:
    ORDER_NR; DESCRIPTION; CREATE_DATE; A_STAT_AGE; R_STAT_AGE; U_STAT_AGE
    A500236; OWN HOME; 20/02/2012; 5; 3; 1
    A623555; REPAIR CAR; 01/10/2012; 42; 39; 38
    A866944; MAINTAIN EQUIPMENT; 11/02/2012; 15; 4; 3

    The age of my query result should be calculated from the date of the creation of the order.
    I would like to know 2 things, one is how old was the order when they reached this status, R and U.
    The second, that is, how long did you order remaining on A stat, R and U (and if possible all the others too)
    It can happen that not every order reaches every State, so he ca go directly from A you in this case I want to display a generic character in this row/column

    I hope you all know what I mean and what result to expect.

    Thanks for your help.

    Reinhard W.

    Hi, Reinhard,

    990524 wrote:
    Hi Frank,.

    I thank you for your professional response and excuse my non-professional way to clarify my question.
    I have now already read and understand how to ask good questions, but is there an easy an inexpensive way to run a database on my computer at home?

    You can download the Oracle database from this site. The Express edition is easier to install. It lacks a few features that the Enterprise edition (for example, partitioning table and safety of level line), but most of the things work in all editions of Oracle.
    All editions are free if you do not use them for Production applications. Of course, at this price (or lack thereof), you get Oracle support.

    I have already noticed that the syntax SQL for Oracle differ from other DBMSS.

    Yes and some features are different in different products.

    Your query works perfectly, thank you.

    Now I want to refine my (your) query, is there a way to display a range of day within 1 day? As 0.5 day to half a day between two State timestamp.

    Of course, you can do it. After the exact output desired of the sample data that I posted. If the sample data do not show what you need to do, then after a few different examples of data that makes.

    My table contains the Date and time in a row, I forgot to mention that.

    After a CREATE TABLE statement. I was guessing a lot of things that may be important. For example, I assumed you were using a DATE for date and time column. So it is only reasonable to do so, but maybe it's not what looks like at your table.

    I tried something like this:

    SYSDATE - MIN ( CASE
                        WHEN  t2.wo_step = 'A'
                  THEN  t2.step_date
              END
               ) AS Age_A
    

    But it gives me an error of arithmetic overflow and not die difference in the Date and time or days with decimal friction.

    There are only about 5.4 million possible days with DATEs of Oracle. Is only 7 digits and Oracle can treat approximately 39 numbers without overflow errors, so I don't see how this can happen if t2.step_date is really a DATE. This is an example of why you need to publish a CREATE TABLE statement.

  • Join with conditions of outer join statement

    Hello
    The following was extracted from the 11 GR 2 document guide performance, which I am not quite understand.
    Can anyone provide some examples to clearly explain this. Thank you

    For join instructions examples with outer join conditions, the table with the outer join operator must come after the other table in the condition in the join order. The optimizer does not consider join orders that violate this rule. Similarly, when a subquery was transformed into an antijoin or a semi-join, arrays of the subquery must come after these tables in the outer query to which they were connected or block in correlation. However, semijoins and antijoins of hash are override this order condition in certain circumstances

Maybe you are looking for