Different Outer Join query result

Hi all

Under query gives the expected result


Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE (+) = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. '


But the following query does not give the expected result.


Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. '

Difference between them is a join with external FPOVR. LEVEL_VALUE and hard coded value. Why these query above return different results even if the outer join is made with hard coded in value?

Thank you
SK

SK says:
Hi all

Under query gives the expected result

Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE (+) = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. '

But the following query does not give the expected result.

Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. '

Difference between them is a join with external FPOVR. LEVEL_VALUE and hard coded value.

In fact, the difference is that the condition relating to FPOVR. LEVEL_VALUE to 21533 is part of the outer join condition in the first query, but it is not in the second query.

Why these query above return different results even if the outer join is made with hard coded in value?

When you say

FPOVR.LEVEL_VALUE(+) = 21533

(with a sign +) only the lines of fpovr with level_value = 21533 will join the other table. If there is no such line in fpovr, then a row of output will be generatedd for each row in the other table anyway.
When you say

FPOVR.LEVEL_VALUE = 21533

(without a sign +) then the outer join is made using the other conditions, which may result in some lines of the other table who appear in the result set with NULL in places where the values of fpovr would normally appear. Then when the join is complete, the condition is applied, and all these lines with the NULL value instead of fpovr.level_value are deleted because they do not meet the condition "FPOVR. LEVEL_VALUE = 21533 ". The effect is identical to an inner join.

When you use the old rating of outer join (i.e. with signs +), if any condition involving the fpovr table has a + sign, and then all of the conditions involving table fpovr must have a + sign, otherwise the result will be the same if none of the conditions were a sign +.

Tags: Database

Similar Questions

  • OUTER JOIN query returns the results of JOIN IN-HOUSE 11.2.0.1.0

    I'm data transfer in 11.2.01.0 (Windows XP 32-bit) and I wanted to compare the sizes of table with the same Table name in two different patterns, ML and SILENT, with a FULL OUTER JOIN (to account for all the tables and NULL values in a diagram).

    The scheme of ML has 176 tables: schema TUT a 133 tables. The use of a standard INNER JOIN gives 131 paintings.

    I get precisely the results with a FULL OUTER JOIN I get with an INTERNAL JOIN (not the same NULL values so I know they exist).

    This happens in SQL-Plus, SQL_Developer and using Oracle Wire pilot of Data_Direct.

    Here is the code:

    Login: SYS as SYSDBA or SYSTEM (same results for either)

    SELECT M.TABLE_NAME, M.NUM_ROWS, T.TABLE_NAME, T.NUM_ROWS
    OF SYS. ALL_TABLES M FULL OUTER JOIN SYS. ALL_TABLES T ON M.TABLE_NAME = T.TABLE_NAME
    WHERE
    M.OWNER = 'ML' AND
    T.OWNER = 'TUT';

    Produce the same results with LEFT OUTER joins and RIGHT OUTER joins in ASI and Oracle (+) syntax.

    Any thoughts?

    Hello

    If you read what I posted, forget it. MScallion (below) gave the correct answerr.

    If conditions such as

    owner   = 'ML'
    

    in the WHERE clause, and then they will reject the rows of the result set formed by the join condition.

    The inner join returns only 131 lines where the two 'paintings' have the same table_names.
    The outer joins return multiple lines (133, 176 or 178) before the place WHERE the provision is applied , but the WHERE clause eliminates all lines except the 131 found by the inner join.

    Published by: Frank Kulash, July 10, 2010 14:23

  • need help for an outer join query

    Hi friends...
    I have oracle 10g...
    I have a question which involve is joining three tables...

    the query is as follows:
    SELECT DISTINCT MU.MKT_ID                  "PAR ID",
                       MU.MKT_ID                  "ROOT ID",
                     MSU.SPEC_ID                "ID",
                   SU.SPEC_DESC                "DESC",
               SU.SPEC_CD          "SPEC CD"
    
                  FROM IPOADM_BATCH.MKT_UV1        MU,
                         IPOADM_BATCH.MKT_SPEC_UV1   MSU,
                 IPOADM_BATCH.SPEC_UV1       SU
    
                   WHERE MSU.MKT_ID  = MU.MKT_ID
               AND MSU.SPEC_ID = SU.SPEC_ID
             AND SU.SPEC_GRP_OR_CMPSTN_CD = 'C';
    in my front end application has some delete operations that removes specific data of the IPOADM_BATCH. MKT_SPEC_UV1 due to which the above query is nor fetch all rows.
    the relationship between the tables is clearly from the above query...

    I have the data in IPOADM_BATCH. MKT_UV1 and IPOADM_BATCH. Table of SPEC_UV1 after the delete operation

    now, I want the query above to retrieve data from IPOADM_BATCH. MKT_UV1 with small changes to the above query even if there is no data in IPOADM_BATCH. MKT_SPEC_UV1.

    I thought that if I use an outer join I can get it, but here I have to join three tables-join condition is based on the table that doesn't have a data...


    so please help me guys how can I change the query to get my desired out put...

    Hello

    There are several different things you could mean by it.

    Here's how to join the tables to get one of them:

    SELECT DISTINCT  mu.mkt_id                  "PAR ID",
                     mu.mkt_id                  "ROOT ID",
                     msu.spec_id                "ID",
                     su.spec_desc               "DESC",
                     su.spec_cd                   "SPEC CD"
    FROM           ipoadm_batch.mkt_uv1        mu
    LEFT OUTER JOIN      ipoadm_batch.mkt_spec_uv1   msu  ON   msu.mkt_id               = mu.mkt_id
    LEFT OUTER JOIN  ipoadm_batch.spec_uv1       su       ON   msu.spec_id              = su.spec_id
                                                  AND  su.spec_grp_or_cmpstn_cd = 'C'
    ;
    

    If it does not matter what it is that you want, and then after a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) for all the tables and the results desired from these data.
    Highlight a few places where the above querry is the production of incorrect results of your sample data and explains how to get the correct results in these places.

  • 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

  • Concatinating join query results

    Hello

    Is it possible to concatenate the value of a particular column that are the result of a join query.

    Employee
    Employee Id | Employee Name
    254         | Employee 01 
    266         | Employee 02 
    654         | Employee 03
    741         | Employee 04
    266         | Employee 05
    877         | Employee 06
    955         | Employee 07
    Project
    Project Id | Project Name 
    1          | Project 01   
    2          | Project 02       
    3          | Project 03               
    Project_Employee
    Project Id | Employee ID 
    1          | 254         
    1          | 266        
    1          | 654        
    2          | 741        
    2          | 266        
    3          | 877          
    2          | 955        
    Result
    Project Id | Project Name | Employees
    1          | Project 01   | Employee 01,  Employee 02,  Employee 03
    2          | Project 02   | Employee 04, Employee 05, Employee 07
    3          | Project 03   | Employee 06         
    I use oracle 10g

    Thank you

    Hello

    SQL> with employee as (
      2  select 254 employee_id, 'Employee 01' employee_name from dual  union all
      3  select 266, 'Employee 02' from dual  union all
      4  select 654, 'Employee 03' from dual union all
      5  select 741,  'Employee 04' from dual union all
      6  select 266, 'Employee 05' from dual union all
      7  select 877, 'Employee 06'from dual union all
      8  select  955,         'Employee 07' from dual),
      9  project as (
     10  select  1 project_id,'Project 01' project_name  from  dual union all
     11  select 2 , 'Project 02' from dual  union all
     12  select 3, 'Project 03' from dual),
     13  project_employee as(
     14  select 1 project_id,         254  employee_id from dual union all
     15  select 1 ,         266  from  dual union all
     16  select 1 ,654 from  dual union all
     17  select 2,741  from dual union all
     18  select 2 , 266   from dual  union all
     19  select 3 , 877  from dual union all
     20  select 2 ,955 from dual),
     21  a  as (select  p.project_id, p.project_name, e.employee_id, e.employee_name
    
     22              from employee e, project p, project_employee pe
     23              where pe.project_id = P.project_id
     24              and pe.employee_id = e.employee_id)
     25              select a.project_id proj_id  , a.project_name proj_name,
     26                    rtrim(xmlagg(xmlelement("enam",
     27                                         a.employee_name || ', ')
     28                    order by employee_id).extract('//text()'),', ')
     29                     as employee_names
     30              from a
     31              group by a.project_id,a.project_name;
    
       PROJ_ID PROJ_NAME
    ---------- ----------
    EMPLOYEE_NAMES
    --------------------------------------------------------------------------------
    
             1 Project 01
    Employee 01, Employee 02, Employee 05, Employee 03
    
             2 Project 02
    Employee 02, Employee 05, Employee 04, Employee 07
    
             3 Project 03
    Employee 06
    
    SQL>
    

    The output may be formatted, but the result is what you want.

  • Left outer join query and check the status

    Hello

    I have two tables as tables below

    table_1

    MI_ACC_IDENTIFIERCHARGE_START_DATECHARGE_END_DATEPBA_INT_AMT
    200000000001 SEP-05 00.00.0029 SEP-05 00.00.000.26
    200000000030 SEP-05 00.00.0031 OCTOBER 05 00.00.001.92
    20000000001 NOVEMBER 05 00.00.00NOVEMBER 30 05 00.00.000.34
    20000000001 AUGUST 06 00.00.0031 AUGUST 06 00.00.000.47
    200000000031 MARCH 06 00.00.0027 APRIL 06 00.00.000.34
    200000000030 DECEMBER 05 00.00.0031 JANUARY 06 00.00.001.92
    20000000001 MARCH 05 00.00.0031 MARCH 05 00.00.000.26

    Table_2

    MI_ACC_IDENTIFIERCHARGE_START_DATECHARGE_END_DATETOT_INT_AMT_OVER_25P
    200000000030 SEP-05 00.00.0031 OCTOBER 05 00.00.000
    20000000001 NOVEMBER 05 00.00.00NOVEMBER 30 05 00.00.000.81756
    20000000001 DECEMBER 05 00.00.0029 DECEMBER 05 00.00.000.64724
    200000000030 DECEMBER 05 00.00.0031 JANUARY 06 00.00.005.51555

    Power required:

    MI_ACC_IDENTIFIERCHARGE_START_DATECHARGE_END_DATENVL(B.PBA_INT_AMT,0)TOT_INT_AMT_OVER_25P
    20000000001 NOVEMBER 05 00.00.00NOVEMBER 30 05 00.00.000.340.81756
    200000000030 DECEMBER 05 00.00.0031 JANUARY 06 00.00.001.925.51555
    20000000001 DECEMBER 05 00.00.0029 DECEMBER 05 00.00.0000.64724

    I have to check if TOT_INT_AMT_OVER_25P > B.PBA_INT_AMT and also required to display if there is no matching record in table_1 and exist in table_2 then display as well

    Queries for the table:

    CREATE TABLE 'TABLE_1 '.

    (

    ACTIVATE THE "MI_ACC_IDENTIFIER" NUMBER (10,0) NOT NULL,

    DATE OF THE "CHARGE_START_DATE."

    DATE OF THE "CHARGE_END_DATE."

    NUMBER (15.2) "PBA_INT_AMT".

    );

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date ('01 - SEP - 05 00.00.00','DD-MON-RR HH24.MI.)) SS'), to_date (29-SEP-05 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.26);

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date (30-OCT-05 00.00.00','DD-MON-RR HH24.MI.)) SS'), to_date (31 October 05 00.00.00','DD-MON-RR HH24.MI.) SS'), 1.92);

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date (1 November 05 00.00.00','DD-MON-RR HH24.MI.)) To_date SS'), (30 November 05 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.34).

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date (1 August 06 00.00.00','DD-MON-RR HH24.MI.)) SS'), to_date (31 August 06 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.47);

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date (31 March 06 00.00.00','DD-MON-RR HH24.MI.)) To_date SS'), (27 April 06 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.34).

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date (30 December 05 00.00.00','DD-MON-RR HH24.MI.)) To_date SS'), (31 January 06 00.00.00','DD-MON-RR HH24.MI.) SS'), 1.92);

    Insert into TABLE_1 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, PBA_INT_AMT) values (2000000000, to_date (1 March 05 00.00.00','DD-MON-RR HH24.MI.)) SS'), to_date (31 March 05 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.26);

    CREATE TABLE 'TABLE_2.

    (

    ACTIVATE THE "MI_ACC_IDENTIFIER" NUMBER (10,0) NOT NULL,

    DATE OF THE "CHARGE_START_DATE."

    DATE OF THE "CHARGE_END_DATE."

    "TOT_INT_AMT_OVER_25P" NUMBER (15.5)

    );

    Insert in TABLE_2 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, TOT_INT_AMT_OVER_25P) values (2000000000, to_date (30-OCT-05 00.00.00','DD-MON-RR HH24.MI.)) SS'), to_date (31 October 05 00.00.00','DD-MON-RR HH24.MI.) SS'), 0);

    Insert in TABLE_2 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, TOT_INT_AMT_OVER_25P) values (2000000000, to_date (1 November 05 00.00.00','DD-MON-RR HH24.MI.)) To_date SS'), (30 November 05 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.81756);

    Insert in TABLE_2 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, TOT_INT_AMT_OVER_25P) values (2000000000, to_date (1 December 05 00.00.00','DD-MON-RR HH24.MI.)) SS'), to_date (29 December 05 00.00.00','DD-MON-RR HH24.MI.) SS'), 0.64724);

    Insert in TABLE_2 (MI_ACC_IDENTIFIER, CHARGE_START_DATE, CHARGE_END_DATE, TOT_INT_AMT_OVER_25P) values (2000000000, to_date (30 December 05 00.00.00','DD-MON-RR HH24.MI.)) To_date SS'), (31 January 06 00.00.00','DD-MON-RR HH24.MI.) SS'), 5.51555);

    Query, I used:

    SELECT A.MI_ACC_IDENTIFIER, A.CHARGE_START_DATE, A.CHARGE_END_DATE, NVL (B.PBA_INT_AMT, 0), a.TOT_INT_AMT_OVER_25P OF
    TABLE_2 A, B FROM TABLE_1
    WHERE A.MI_ACC_IDENTIFIER = B.MI_ACC_IDENTIFIER (+)
    AND A.CHARGE_START_DATE = B.CHARGE_START_DATE (+)
    AND A.CHARGE_END_DATE = B.CHARGE_END_DATE (+)
    and A.TOT_INT_AMT_OVER_25P > B.PBA_INT_AMT (+);

    I have been using the syntax of ANSI join for some time. They are readable and code looks more elegant. A reason do not want to use it?

    I don't care

    SQL> select t2.mi_acc_identifier
      2       , t2.charge_start_date
      3       , t2.charge_end_date
      4       , nvl(t1.pba_int_amt, 0) pba_int_amt
      5       , t2.tot_int_amt_over_25p
      6    from table_1 t1
      7       , table_2 t2
      8   where t1.mi_acc_identifier (+)= t2.mi_acc_identifier
      9     and t1.charge_start_date (+)= t2.charge_start_date
     10     and t1.charge_end_date   (+)= t2.charge_end_date
     11     and (t1.pba_int_amt < t2.tot_int_amt_over_25p     or t1.mi_acc_identifier is null);
    
    MI_ACC_IDENTIFIER CHARGE_ST CHARGE_EN PBA_INT_AMT TOT_INT_AMT_OVER_25P
    ----------------- --------- --------- ----------- --------------------
           2000000000 01-NOV-05 30-NOV-05         .34               .81756
           2000000000 30-DEC-05 31-JAN-06        1.92              5.51555
           2000000000 01-DEC-05 29-DEC-05           0               .64724
    
    SQL>
    
  • outer join query

    Hi, here is the code to test
    create table dm_top (site varchar2(10),
    area varchar2(10),
    machine varchar2(10),
    operation varchar2(10));
    
    create table dm_mid (site varchar2(10),
    area varchar2(10),
    machine varchar2(10),
    operation varchar2(10),
    activity varchar2(10));
    
    create table dm_tr(site varchar2(10),
    area varchar2(10),
    machine varchar2(10),
    operation varchar2(10),
    dm_tr_id number); 
    INSERT orders
    insert into dm_top
      (site, area, machine, operation)
    values
      ('site1','area1','machine1','oper1');
      
      insert into dm_top
      (site, area, machine, operation)
    values
      ('site1','area1','machine1','oper2');
      
    insert into dm_mid
      (site, area, machine, operation, activity)
    values
      ('site','area1','machine1','oper1','act1');
    
      
     insert into dm_mid
      (site, area, machine, operation, activity)
    values
      ('site','area1','machine1','oper1','act2');
      
      insert into dm_mid
      (site, area, machine, operation, activity)
    values
      ('site','area1','machine1','oper1','act3');
      
    
      
      insert into dm_mid
      (site, area, machine, operation, activity)
    values
      ('site','area1','machine1','oper2','act1');
    
      
     insert into dm_mid
      (site, area, machine, operation, activity)
    values
      ('site','area1','machine1','oper2','act2');
      
      insert into dm_mid
      (site, area, machine, operation, activity)
    values
      ('site','area1','machine1','oper2','act3');
      
      
    insert into dm_tr
      (site, area, machine, operation, dm_tr_id)
    values
      ('site','area1','machine1','oper1',9999);
    and my desired output, always had to print even if there is no record in dm_tr
    site area1 machine1 oper1 act1  9999
    site area1 machine1 oper1 act2  9999
    site area1 machine1 oper1 act3  9999
    site area1 machine1 oper2 act1 
    site area1 machine1 oper2 act2
    site area1 machine1 oper2 act3
    Thanks in advance.

    Try this

    select a.*,c.draft, b.dm_tr_id from dm_mid a,dm_tr b,dm_top c
    where a.site = b.site (+) and
    a.area = b.area (+) and
    a.machine = b.machine (+) and
    a.operation = b.operation (+) and
    a.area = c.area (+) and
    a.machine = c.machine(+) and
    a.operation = c.operation(+)
    order by 1,2,3,4,5
    /
    
    SITE       AREA       MACHINE    OPERATION  ACTIVITY   D   DM_TR_ID
    ---------- ---------- ---------- ---------- ---------- - ----------
    site       area1      machine1   oper1      act1       Y       9999
    site       area1      machine1   oper1      act2       Y       9999
    site       area1      machine1   oper1      act3       Y       9999
    site       area1      machine1   oper2      act1       N
    site       area1      machine1   oper2      act2       N
    site       area1      machine1   oper2      act3       N
    
    6 rows selected.
    
    Elapsed: 00:00:00.00
    

    Kind regards
    Prazy

  • My Confusion of outer join

    Oracle 10g

    I have three below queries that use the outer join syntax.

    All three queries return exactly the same results


    (a)
    SELECT TA.ID TA
         , TB.ID TB
      FROM TA
         , TB
     WHERE TA.ID = TB.ID(+);
    (b)
    SELECT TA.ID TA
         , TB.ID TB
      FROM TA
         , TB
     WHERE TB.ID(+) = TA.ID;
    (c)
    SELECT TA.ID TA
         , TB.ID TB
      FROM TA
      LEFT OUTER JOIN TB ON TA.ID = TB.ID;
    I have the right call outer join query because the (+) sign is located on the right and outer join query (b) left because the (+) sign is on the left?

    Or is the left join or right determined by the join columns specified in the order select it?



    Create the Script
    CREATE TABLE TA
      (
        "ID" NUMBER
      );
    
    CREATE TABLE TB
      (
        "ID" NUMBER
      );
    
    INSERT INTO TA (ID) VALUES ('1');
    INSERT INTO TA (ID) VALUES ('2');
    INSERT INTO TA (ID) VALUES ('3');
    
    INSERT INTO TB (ID) VALUES ('1');
    INSERT INTO TB (ID) VALUES ('2');
    INSERT INTO TB (ID) VALUES ('4');
    Ben

    Published by: benton on August 15, 2012 08:16

    Hi, Ben.

    Benton says:
    ... So is it correct to say that the left or right refers to which side the null values will be displayed?

    Lol it is incorrect to say that the old syntax outer join (using the sign +) is either a left - or a right outer join.

    ... So I need to have the order of the columns in the correct SELECTION so that there is no likelihood of confusion over which side will display NULL values. If I place the columns A and B in the wrong order, that is to say B then a I'll lend to confusion about what will be returned with respect whether left or right.

    No, do not hesitate to organize columns in the select in any way will help your users the most. What is happening in the FROM and WHERE clause, in particular the order in which the tables happens to appear, may not have something to do with the order of the columns in the output.
    Many readers are more comfotable with having NULL columns at the end of a line of output, or at least not at the beginning, so maybe it's one of the reasons for ta.id first in your example. Rearrange the columns in the game any way more than makes sense for people who will look at them.

  • That IS NULL is done by replacing the NOT EXISTS operator with an outer join?

    DB version: 10 gr 2

    An example of searchoracle.target.com

    The query that contains a NOT EXISTS operator
    SELECT M.ModulId, M.Modul FROM MODULE M
    WHERE NOT EXISTS
    ( SELECT PROJEKTMODUL.IdModul 
      FROM PROJEKTMODUL 
      WHERE M.ModulId = PROJEKTMODUL.IdModul 
      AND PROJEKTMODUL.IdProjekt = 23 )
    is replaced by an equivalent outer join query
    select distinct
           M.ModulId
         , M.Modul 
      from MODULE M
    left outer
      join PROJEKTMODUL
        on M.ModulId = PROJEKTMODUL.IdModul
       and PROJEKTMODUL.IdProjekt = 23
     where PROJEKTMODUL.IdProjekt is null
    I do not understand what
    PROJEKTMODUL.IdProjekt is null
    fact in the above rewritten query.

    He is to perform an anti-jointure. As far as I know Oracle (starting with version 10g - I thought that release 1) rewritten a Join Anti that resembles your second query of your request NOT EXISTS and NOT IN.

    Look at the results of the query without the predicate IS NULL and see what records (from this set) must be selected to show the same results as the first query. Areas of interest match the predicate IS NULL.

  • Outer join does not not as expected left

    Hi all

    I have it here are three tables with values. Mentioned the under outer join query does not and behave like the inner query.

    CREATE TABLE RET_FUND_FEE

    (

    NPTF VARCHAR2 (8 CHAR),

    TPART VARCHAR2 (4 CHAR)

    );

    CREATE TABLE PART_PTF

    (

    Mf_Id VARCHAR2 (6 CHAR) NOT NULL,

    TPARTS VARCHAR2 (4 CHAR) NOT NULL

    );

    CREATE TABLE TFC_FUNDS

    (

    NPTF VARCHAR2 (8 CHAR) NOT NULL,

    MULTIFONDS_ID VARCHAR2 (6 CHAR)

    );

    INSERT INTO RET_FUND_FEE VALUES('111','A');

    INSERT INTO RET_FUND_FEE VALUES('111','D');

    INSERT INTO RET_FUND_FEE VALUES('111','E');

    INSERT INTO PART_PTF VALUES ('MF1', 'A');

    INSERT INTO PART_PTF VALUES ('MF1', 'B');

    INSERT INTO PART_PTF VALUES('MF1','C');

    INSERT INTO TFC_FUNDS VALUES('111','MF1');

    INSERT INTO TFC_FUNDS VALUES('111','MF1');

    INSERT INTO TFC_FUNDS VALUES('111','MF1');

    SELECT A.TPART, B.TPARTS, A.NPTF, B.Mf_Id, C.MULTIFONDS_ID, C.NPTF

    OF RET_FUND_FEE A, PART_PTF B, TFC_FUNDS C

    WHERE A.NPTF = C.NPTF

    AND C.MULTIFONDS_ID = B.Mf_Id

    AND A.TPART = B.TPARTS (+)

    AND C.MULTIFONDS_ID = 'MF1 '.

    AND C.NPTF = '111'

    Here, I expect all records in the RET_FUND_FEE table that I am using outer join.

    But I'm only corresponding chronogram RET_FUND_FEE, PART_PTF as an inner join. Can you get it someone please let me know what lack us.

    Is my version of oracle 11g

    SELECT

    A.TPART, B.TPARTS, A.NPTF, B.Mf_Id, C.MULTIFONDS_ID, C.NPTF

    Of

    PART_PTF B

    Join

    C TFC_FUNDS

    on (C.MULTIFONDS_ID = B.Mf_Id

    AND C.MULTIFONDS_ID = 'MF1 '.

    AND C.NPTF = '111'

    )

    right outer join

    RET_FUND_FEE HAS

    on (A.TPART = B.TPARTS

    and A.NPTF = C.NPTF) - added as correction

    TPART TPARTS NPTF MF_ID MULTIFONDS_ID NPTF
    A A 111 MF1 MF1 111
    A A 111 MF1 MF1 111
    A A 111 MF1 MF1 111
    E - 111 - - -
    D - 111 - - -

    or

    SELECT A.TPART, d.TPARTS, A.NPTF, d.Mf_Id, d.MULTIFONDS_ID, d.NPTF

    OF RET_FUND_FEE HAS

    , (

    Select

    *

    PART_PTF b, TFC_FUNDS C

    where B.Mf_Id = C.MULTIFONDS_ID

    AND C.MULTIFONDS_ID = 'MF1 '.

    AND C.NPTF = '111'

    ) d

    WHERE A.TPART = D.TPARTS (+)

    and A.NPTF = D.NPTF (+) - added as a correction

    Sorry had to correct the syntax oracle solution.

    The first one was bad because it would return also B lines that have no match in C.

    Sorry a correction more on these two approaches, missed the second predicate.

  • Bad result in a left outer join in 12.1.0.2

    Hallo,

    We discovered a strange behaviour in a query. The query provides values in a column of outer join where there is no corresponding value in the table is attached to the outside.

    When you expand this request by the "ORDER BY" then this query gives the correct result.

    Example:

    SQL > desc tb_a
    Name                                Null?    Typ
    -------------------------------------------- ----------------------------
    ID NOT NULL NUMBER (19)

    SQL > desc tb_b
    Name                                Null?    Typ
    -------------------------------------------- ----------------------------
    CLOSED NOT NULL NUMBER (1)
    ID NOT NULL NUMBER (19)

    CCS_APPLICATION@icw01> select * from tb_a where id in (4148,4141,4195);

    ID
    ----------
    4148
    4141
    4195

    CCS_APPLICATION@icw01> select * from tb_b where id in (4148,4141,4195);

    INTERNAL ID
    ---------- ----------
    4148 0

    CCS_APPLICATION@icw01> SELECT
    2      b.id                            AS b_id,
    3      a.id                            AS a_id,
    4 b.closed AS b_closed
    5
    6 tb_a a
    7 LEFT OUTER JOIN tb_b b ON a.id = b.id
    8 WHERE a.id IN (4148, 4195, 4141)
    9 ORDER BY ASC a.id
    10;

    B_ID ALLOCATION A_ID B_CLOSED
    ---------- ---------- ----------
    4141
    4148 4148 0
    4195

    CCS_APPLICATION@icw01> SELECT
    2      b.id                            AS b_id,
    3      a.id                            AS a_id,
    4 b.closed AS b_closed
    5
    6 tb_a a
    7 LEFT OUTER JOIN tb_b b ON a.id = b.id
    8 WHERE a.id IN (4148, 4195, 4141)
    9 - ORDER BY ASC a.id
    10;

    B_ID ALLOCATION A_ID B_CLOSED
    ---------- ---------- ----------
    4148 4148 0
    4141 4141
    4195 4195

    instance parameter:

    VALUE OF TYPE NAME

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

    compatible string 12.1.0.2.0

    optimizer_features_enable string 12.1.0.2

    After ""alter system set optimizer_features_enable = ' 11.2.0.4 ';"  the query provides the correct result in both cases (ordered and unordered).

    Now the final question: is this a bug?

    1480970 wrote:

    Hallo!  Yes, I searched the Support of Oracle. I found some similar entries, but not an exact match. To fix some issues

    with 12.1.0.2.

    There is another interesting clue when look you on the execution plan:

    Note

    -----

    -the dynamic statistics used: dynamic sampling (level = 2)

    - This is an adaptation plan

    We have disabled (= FALSE) optimizer_adaptive_features and the query provides the correct values.

    This could be a solution for us.

    Looks like a pretty tight match for bug 18430870, even if it affects the two 12.1.0.1 and 12.1.0.2, which contradicts the Martin trial against 12.1.0.1.

    The description of the bug mentions disabling "_projection_pushdown" (set to false) should also be a viable solution, perhaps if you want to give that a try and see if it is a different bug or not.

    There are also a number of one-time fixes already available for download, maybe your version / platform is already covered, if the bug applies.

    Randolf

  • outer join on the aggregate query

    This is probably a relatively simple matter, as long as I explain it well enough:

    I have two tables:

    categorycodes and properties

    categorycodes is a lookup table.

    both tables have a catcode field which is a char (1) that contains matching data (only the numbers 1 to 6)

    CREATE

    TABLE CATEGORYCODES

    (

    CATCODE CHAR (1 BYTE) NOT NULL,

    DESCRIPTION VARCHAR2 (25 BYTE) NOT NULL,

    CONSTRAINT CATEGORYCODES_PK PRIMARY KEY (CATCODE) ALLOW

    )

    catCode

    1

    2

    3

    4

    5

    6

    The properties table has approximately 600 000 records. The properties table also has a field named parcelno which is a tank (9).  It contains a string of figures and numbers only.

    What I would like is:

    catCode, count (*)

    1 580

    2 300

    3 3000

    4 235

    5 0

    6 80

    I limited the results of the query to make sure it was a game that would not all catcodes in it.  I have trouble to get the one with zero to display.  I know that this has to do with how I do the join, but I don't know what.

    It is a sample of what I've tried:

    Select i.

    Of

    (select catcode, count (*)

    property p

    where substr (parcelno, 1, 3) = ' 871 "

    Catcode group) i

    outer join right categorycodes cc

    We i.catcode = cc.catcode;

    I'm not worried about the situations where catcode is null in the properties.  Parcelno cannot be null.

    Hello

    Looks like your query should work; except that you won't COUNT (*); That would make each issue at least 1.  COUNT (*) means that count the total number of lines, no matter what is on them, so he'll see the line with just the catcode of the lookup table that matches nothing and which count as 1.  You want to count the number of rows in the table of properties, so expect a column of the properties that cannot be NULL.

    Here is a slightly different way

    SELECT c.catcode

    EARL of (p.catcode) AS cnt

    OF categorycodes c

    P ON p.catcode = c.catcode LEFT OUTER JOIN properties

    AND SUBSTR (p.parcelno

    1

    3

    ) = ' 871 "

    ;

    If the condition about 871' ' part of the join condition, then you don't need a subquery.

    .

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
    Point where the above statement is erroneous results, and explain, using specific examples, how you get the right result of data provided in these places.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

  • outer join does not give the expected results (missing documents)

    Hello
    I have problem with outer join in OBIEE 10 g.

    I'm testing it on the model of simplified database

    STAT table with some statistics of TYPEs
    stat_date, id_type, num
    ..
    ..
    26/03/2003, 20: 1
    26/03/2003, 21: 1
    26/03/2003, 23: 1
    26/03/2003, 24, 1
    26/03/2003, 25, 1
    ..
    27/03/2003, 22, 1 <-different date here
    ..
    ..

    Table TYPES
    ID_type
    ..
    ..
    20
    21
    22
    23
    24
    25
    ..
    ..


    Physical model
    STAT >-TYPES


    AM.
    F1 (only Digital STAT table column (agg. County))
    D1 (STAT_DATE of the STAT table column only)
    D2 (source TYPES)

    D1 - inner join - < F1
    D2 - left outer join - < F1


    I would like to create simple report filtered by date with number of each type.
    I want to get this result
    ID_type County
    ..     ..
    ..     ..
    20 1
    21 1
    22 null
    23 1
    24 1
    25 1
    ..     ..
    ..     ..


    But I get this
    ID_type County
    ..     ..
    ..     ..
    20 1
    21 1
    23 1
    24 1
    25 1
    ..     ..
    ..     ..

    ID_type 22 is missing


    !!!

    I NORMALLY GET RESULT ONLY IF I DELETE RECORD ' 27/03/2003 22' OF MY FACT TABLE.

    !!!


    I have the filter on the date column D1
    STAT_DATE = 26/03/2003 or STAT_DATE is null


    BI Server running this SQL query


    Select distinct D1.c1 as c1,
    D1. C2 C2
    Of
    (select count (T17840. NUM) in c1.
    T17867.ID_TYPE C2
    Of
    Left outer join of TYPES T17867
    STAT T17840 on T17840.ID_TYPE = T17867.ID_TYPE
    where (T17840. STAT_DATE in (TO_DATE ('2003-03-26', 'YYYY-MM-DD')) or T17840. STAT_DATE is null)
    T17867.ID_TYPE group
    ) D1


    Can you give me some advice?

    Thank you

    Hi cardel,

    I think that you can achieve your goal if you build a dimension of the array STAT form, you can create a table Alias in the physical layer and then to join the table of facts (external to the left). In this way, I think that you can solve your problem.

    Let me know.

    Kind regards
    Gianluca

  • FULLT OUTER JOIN on two different selects

    Hello

    I am facing a problem where I don't just get the handle to solve.

    I have two pieces of music with different return values. This value should be compared and joined. Simply put, I do an example where the return of the selection is represented as the table1 and table2
    Here is the example:
    table1
    abc_comp_nr, abc_cnt_emp
    12000, 15
    12500, 10
    13000, 30
    13500, 10
    
    table2
    xyz_comp_nr, xyz_cnt_emp
    12000, 15
    13000, 30
    14000, 20
    
    Output should be this:
    comp_nr, abc_cnt_emp, xyz_cnt_emp
    12000, 15, 15
    12500, 10, 0
    13000, 30, 30
    13500, 10, 0
    14000, 0, 20
    comp_nr is the combination of abc_comp_nr or xyz_comp_nr

    I tried this but did not work as expected:
    Select?, abc_cnt_emp, xyz_cnt_emp
    Of
    Table1 FULL OUTER JOIN table2
    ON abc_comp_nr = xyz_comp_nr

    Hope that someone was in trouble similar and found a good way to work it.

    Thanks in advance

    Tobias

    Hi, Tobias,.

    Tobias Arnhold wrote:
    Hello

    I am facing a problem where I don't just get the handle to solve.

    I have two pieces of music with different return values. This value should be compared and joined. Simply put, I do an example where the return of the selection is represented as the table1 and table2
    Here is the example:

    table1
    abc_comp_nr, abc_cnt_emp
    12000, 15
    12500, 10
    13000, 30
    13500, 10
    
    table2
    xyz_comp_nr, xyz_cnt_emp
    12000, 15
    13000, 30
    14000, 20
    

    Whenever you have a problem, post CREATE TABLE and INSERT statements for the sample data.
    See the FAQ forum {message identifier: = 9360002}

    Output should be this:
    comp_nr, abc_cnt_emp, xyz_cnt_emp
    12000, 15, 15
    12500, 10, 0
    13000, 30, 30
    13500, 10, 0
    14000, 0, 20
    

    comp_nr is the combination of abc_comp_nr or xyz_comp_nr

    I tried this but did not work as expected:
    Select?, abc_cnt_emp, xyz_cnt_emp
    Of
    Table1 FULL OUTER JOIN table2
    ON abc_comp_nr = xyz_comp_nr

    Instead of??? you want to

    NVL (abc_comp_nr, xyz_comp_nr)
    

    The first column is supposed to be the common identifier, abc_comp_nr, or xyz_comp_nr. It doesn't matter which, since the join condition

    ON abc_comp_nr = xyz_comp_nr
    

    said they will be the same, except that it's a FULL OUTER JOIN, if one of them might be null. If you want to display the non NULL when one of them is NULL and or the other (any) when they are both present. It's just that NVL.

    You can use NVL on other columns, too, to display 0 instead of NULL when there is no match:

    SELECT   NVL ( abc_comp_nr, xyz_comp_nr)     AS comp_nr
    ,        NVL ( abc_cnt_emp, 0)               AS abc_cnt_emp
    ,      NVL ( xyz_cnt_emp, 0)               AS xyz_cnt_emp
    FROM              table1
    FULL OUTER JOIN  table2  ON  abc_comp_nr  = xyz_comp_nr
    ;
    

    However, this will display 0 instead of NULL for abc_cnt_emp and xyz_cnt_emp when these columns are NULL for a reason, not just at the time when they do not reach the join condition. If the original paintings contain NULL values and you should keep NULL values in the result set, but you want NULLs which were created by the outer join to show as 0, then you may want something like this:

    SELECT   NVL ( abc_comp_nr, xyz_comp_nr)     AS comp_nr
    ,        NVL2 ( abc_comp_nr, abc_cnt_emp, 0)     AS abc_cnt_emp
    ,      NVL2 ( xyz_comp_nr, xyz_cnt_emp, 0)     AS xyz_cnt_emp
    FROM              table1
    FULL OUTER JOIN  table2  ON  abc_comp_nr  = xyz_comp_nr
    ;
    

    Whenever you use FULL OUTER JOIN, there is an excellent chance that you will want to use NVL (or his brother younger, stronger, COALESCE, or their cousin NVL2) as well.

    Published by: Frank Kulash on 3 March 2013 14:49

  • How do the query select outer join to a report of the APEX

    Hi all

    I'm Ann.

    I have a select statement that is used to calculate statistics for a month (October 2012 in this example)
    Select ph.phase_number
    sum ((case
    WHEN ph.date_finished IS NULL or ph.date_finished > last_day (TO_DATE (' ' Oct 2012 ', ' MY YYYY' "))
    THEN last_day (TO_DATE (' ' Oct 2012 ', ' MY YYYY' "))
    Of OTHER ph.date_finished
    END)
    (-ph.date_started + 1) / count (def.def_id) as avg_days
    Ph phase_membership
    inner join court_engagement this on ph.mpm_eng_id = ce.engagement_id
    join in-house defendant def on ce.defendant_id = def.def_id
    where def.active = 1
    and ph.date_started < = last_day (TO_DATE (' ' Oct 2012 ', ' MY YYYY' "))
    and ph.active = 1
    and UPPER (ce.court_name) LIKE '% '.
    Rollup Group (phase_number)
    ;

    The result is as below
    Phase_Number AVG_DAYS
    Phase One 8.6666666666666667
    Phase two 14.6
    Phase three 12
    11.4615365

    I have another list of selection mainly the list of months between two date value.
    Select to_char (which_month, 'LUN YYYY') as display_month
    de)
    Select add_months (to_date (' ' August 2012 ', ' MY YYYY' "), rownum-1) which_month
    of object
    where
    rownum < = months_between (to_date (' ' Oct 2012 ', ' MY YYYY' "), add_months (to_date (' ' August 2012", "MY YYYY"), - 1))
    order of which_month)

    The query result is as below

    DISPLAY_MONTH

    AUGUST 2012
    SEP 2012
    OCT 2012

    Is it possible I can join these two select statement above to generate a comparable result:

    Days of month Phase number Avg
    August 2012 Phase One 8.666
    Sep 2012 Phase One 7.66
    Oct 2012 Phase One 5,66
    August 2012 Phase two 8.666
    Sep 2012 Phase two 7.66
    Oct 2012 Phase two 5,66
    August 2012 Phase three 8.666
    Sep 2012 Phase three 7.66
    Oct 2012 Phase three 5,66

    Or
    Days of month Phase number Avg
    August 2012 Phase One 8.666
    August 2012 Phase two 7.66
    August 2012 Phase three 5,66
    Sep 2012 Phase One 8.666
    Sep 2012 Phase two 7.66
    Sep 2012 Phase three 5,66
    Oct 2012 Phase One 8.666
    Oct 2012 Phase two 7.66
    Oct 2012 Phase three 5,66

    And it can be controlled by Phase number or month.
    My other colleague suggested I should use a left outer join, but after having tried many ways, I'm still stuck.

    I tried select is
    Select a.display_month, b.* in)
    Select to_char (which_month, 'LUN YYYY') as display_month
    de)
    Select add_months (to_date (' ' August 2012 ', ' MY YYYY' "), rownum-1) which_month
    of object
    where
    rownum < = months_between (to_date (' ' Oct 2012 ', ' MY YYYY' "), add_months (to_date (' ' August 2012", "MY YYYY"), - 1))
    order which_month)) a left outer join

    (Select to_char (ph.date_finished, 'MY YYYY') as join_month, ph.phase_number)
    sum ((case
    WHEN ph.date_finished IS NULL or ph.date_finished > last_day (TO_DATE (a.display_month, 'MY YYYY'))
    THEN last_day (TO_DATE (a.display_month, 'MY YYYY'))
    Of OTHER ph.date_finished
    END)
    (-ph.date_started + 1) / count (def.def_id) as avg_days
    Ph phase_membership
    inner join court_engagement this on ph.mpm_eng_id = ce.engagement_id
    join in-house defendant def on ce.defendant_id = def.def_id
    where def.active = 1
    and ph.date_started < = last_day (TO_DATE (a.display_month, 'MY YYYY'))
    and ph.active = 1
    and UPPER (ce.court_name) LIKE '% '.
    To_char (ph.date_finished, 'MY YYYY'), group (phase_number) rollup) b
    On a.display_month = b.join_month

    but then I get an error
    SQL error: ORA-00904: "A." "" DISPLAY_MONTH ": invalid identifier

    I need to view a report on the APEX with option for people to download at least format CSV.
    Already 1 inteactive report in the page, so I don't think adds another interactive report without using the iframe trick.

    If any of you have any ideas, please help.

    Thank you very much.

    Ann

    Hello Ann,.

    Frank has done a very good job. I am also impressed.

    Is in regard to your correction to his question, the problem is on this replacement you did

    last_day(TO_DATE(am.which_month,'MON YYYY'))
    

    AM.which_month is already a date type, and you don't need to convert it to this day.
    Here is the correct way:

    last_day(am.which_month)
    

    There are also sometimes with the data you've posted have no line for this month. So I also added a function NVL to display 0 under avg_days for these cases.

    Here is my corrected query:

    DEFINE startmonth = "Aug 2012";
    DEFINE endmonth   = "Oct 2012";
    WITH  all_months  AS
    (
       SELECT ADD_MONTHS(to_date('&startmonth','MON YYYY'), ROWNUM-1) AS which_month
       ,      ADD_MONTHS(to_date('&startmonth','MON YYYY'), ROWNUM  ) AS next_month
       from all_objects
       where
       rownum <= months_between(to_date('&endmonth','MON YYYY'), add_months(to_date('&startmonth','MON YYYY'), -1))
    )
    SELECT TO_CHAR (am.which_month, 'Mon YYYY')  AS month
         , ph.phase_number
         , NVL(sum ( (CASE
                     WHEN ph.date_finished IS NULL OR ph.date_finished > last_day(am.which_month)
                     THEN last_day(am.which_month)
                     ELSE ph.date_finished
                  END
                 ) - ph.date_started + 1
               ) / count(def.def_id), 0) as avg_days
      FROM all_months am
           LEFT OUTER JOIN  a_phase_membership  ph  PARTITION BY (ph.phase_number)
              ON  am.which_month <= ph.date_started
              AND am.next_month  >  ph.date_started
              AND ph.date_started <= last_day(am.which_month)  -- May not be needed
              AND ph.active = 1
           LEFT OUTER JOIN  a_engagement  ce
              ON  ph.mpm_eng_id = ce.engagement_id
              AND ce.court_name IS NOT NULL  -- or something involving LIKE
           LEFT OUTER join  a_defendant     def
              ON  ce.defendant_id = def.def_id
              AND def.active = 1
     GROUP BY ROLLUP(phase_number, am.which_month)
     ORDER BY  am.which_month
            ,  ph.phase_number
    ;
    
    The output is:
    MONTH    PHASE_NUMBER           AVG_DAYS
    -------- -------------------- ----------
    Aug 2012 PHASE ONE                     0
    Aug 2012 PHASE THREE                   0
    Aug 2012 PHASE TWO                     0
    Sep 2012 PHASE ONE                    12
    Sep 2012 PHASE THREE                   1
    Sep 2012 PHASE TWO                     9
    Oct 2012 PHASE ONE                     8
    Oct 2012 PHASE THREE                   0
    Oct 2012 PHASE TWO                    14
             PHASE ONE                    11
             PHASE THREE                   1
             PHASE TWO                  11.5
                                  9.71428571
    

    I don't know if that's really what you want. In the case check it and let me know.

    Kind regards.
    Al

Maybe you are looking for

  • Re: Click on Satellite A300 - noisy HARD drive - strange of sound while booting

    Hi all I'm new and I would try to solve some problems with my new laptop * SATELLITE A300-1MZ * (with Vista) and * its hard disk * (a MHZ250BH of Fj-simens). I noticed that he is very mechanical in the normal conditions of use, in fact, when I turn o

  • HP Envy h8 - 1520t - can handle multiple monitors?

    I want to buy an additional monitor or two, but I don't know if my computer can handle them. According to the specifications of my computer, it has an AMD radeon hd gpu 8570.  Is there any specific kind of connection vga, hdmi, dual-link dvi to run t

  • DeskJet 3050 j610 adapter

    Is it possible to buy a power adapter for HP brand for our printer replacement?  I'm always a little wary of electronic purchase on ebay, especially a power cord. Advice?  (HP Deskjet 3050 j610)

  • Dv5000 troubleshooting

    I have an old laptop DV5000 since mid-2006 who worked at random and I'm trying to circumvent the problem. I'll try and keep this short ish since I could go into great detail about every little thing, but will try and cover the main things first and l

  • How RIP an mp3 CD to the library using Windows Media Player?

    Original title: media Player windows How rip CD mp3 in my media library?