Left Join query: revisited... I have to explain at user meeting tomm. pls..

Hi Everyone,
Can someone pls shed some light on the situation below
I am understanding alot of what Michael and Rod wrote.... with my prev. post of LEFT JOIN and testing for
not null and doing a double Boolean OR etc.
- but- AM NOT understanding why the IS NOT NULL works, without the double boolean OR
Pls help... have to explain what left join means to user tomm. I'm going to demo the query below and
not the one with the double boolean OR bec. maybe too much info to present at one sitting. tx, sandra
=====================
the query below is left joining the STUDENT table to
HOLD table.
The HOLD table - contains rows for students who have holds on their record.
a student can have more than one hold (health, HIPAA, basic life saving course)
BUT, for this query: I'm only interested that a hold exists, so I'm choosing MAX on hold desc.
Selecting a MAX, helps me, bec. it reduces my join to a 1 to 1 relationship, instead of
1 to many relationship.
Before I posted this thread at all, the LEFT JOIN below testing for IS NOT NULL worked w/o
me having to code IS NOT NULL twice....
Is that because, what's happening "behind the scenes" is that a temporary table containing all max rows is being
created, for which Discoverer has no predefined join instructions, so it's letting me do a LEFT JOIN and have
the IS NOT NULL condition.
I would so appreciate clarification. I have a meeting on Tues, for which I have to explain LEFT JOINS to the user
and how they should create a query. I need to come up with rules.
If I feel "clear", I asked my boss to buy Camtasia videocast software to create a training clip for user to follow.
Also, if any Banner user would like me to email the DIS query to run on their machine, I would be glad to do so.
thx sooo much, Sandra
SELECT O100384.ACADEMIC_PERIOD, O100255.ID, O100384.ID, O100255.NAME, O100384.NAME, O100255.PERSON_UID, O100384.PERSON_UID, MAX(O100255.HOLD_DESC)
FROM ODSMGR.HOLD O100255, ODSMGR.STUDENT O100384
WHERE ( ( O100384.PERSON_UID = O100255.PERSON_UID(+) ) ) AND ( O100384.ACADEMIC_PERIOD = '200820' )
GROUP BY O100384.ACADEMIC_PERIOD, O100255.ID, O100384.ID, O100255.NAME, O100384.NAME, O100255.PERSON_UID, O100384.PERSON_UID
HAVING ( ( MAX(O100255.HOLD_DESC(+)) ) IS NOT NULL )
ORDER BY O100384.NAME ASC

Hi,
OK, I will try to explain this. When you outer join table B to table A then the rows in table A which do not match any rows in table B will returned with NULL in the columns from the table B.
Oracle uses the syntax ( +) for outer joins. Now if you add another condition using the ( +) syntax (as shown below) the condition will be processed before the table is joined. Therefore if table A does not match any rows in table B which have col2=1 then the row from table A will be returned with NULLs for the table B columns.
SELECT A.col1, B.col1
FROM A, B
WHERE A.col1 = B.col1( +)
AND B.col2( +)=1
Now, if the condition B.col2=1 was used instead then the condition would be processed after the join and therefore the rows from table A that were joined to table B but did not meet the condition would not be returned by the query.
This applies to a WHERE clause and to the HAVING clause, but with one exception. If you use the ( +) within a group function in a HAVING clause then the ( +) will have no affect because the condition must be processed after group by and group by can only be processed after the join. Therefore MAX(B.col2( +)) = 1 is processed after the join even through it uses the ( +) syntax.
You cannot use an OR or an IN with the ( +) syntax because the meaning of the OR in this situation is ambiguous, is the OR done before or after the join. A query with an OR or IN in an outer will fail with an Oracle ORA-01719 error. Discoverer recognises this situation and removes the ( +) so that the error does not occur. However, without the ( +) the conditions are processed after the join.
Using the ( +) with IS NULL, e.g. col2( +) IS NOT NULL works in the same way. You just have to remember that the col2( +) could be NULL as a result of the outer join and therefore if the condition is processed after the query then the IS NOT NULL will remove the outer joined rows.
Hope that is clear.
Rod West

Similar Messages

  • Don'T repeat item with a LEFT JOIN QUERY

    Hello,
    I would like to know how display the following results:
    *Name*:  John Fox
    *Sales:*  1- LAPTOP
                    2- HARDDRIVE
                    3- COMPUTERHere is my 2 tables: CUSTOMER and SALES
    CUSTOMER
    ID NAME GENDER
    1 John Mayer F
    2 Melissa John F
    3 Julie Black F
    4 Mickael Fox M
    5 John Fox M
    SALES
    ID ID_CUSTOMER TYPE
    1 1 Boat
    2 1 TV
    3 4 CD PLAYER
    4 5 LAPTOP
    5 5 HARDDRIVE
    6 5 COMPUTER
    My QUERY
    SELECT customer.Name as NAME, customer.Gender, sales.TYPE
    from customer
    LEFT JOIN sales
    ON customer.ID = sales.ID_CUSTOMER
    WHERE customer.Name = 'John Fox'The problem: If I use the default template, I have:
    NAME GENDER TYPE
    John Fox M LAPTOP
    John Fox M HARDDRIVE
    John Fox M COMPUTER I don'T want the Name John Fox to be repeated at each row.
    I tried to add: #Name# in the REGION DEFINITION - REGION HEADER but I have this result:
    #NAME#
    NAME GENDER TYPE
    John Fox M LAPTOP
    John Fox M HARDDRIVE
    John Fox M COMPUTER
    So, what can I do to have this result? Change the query???
    Name:  John Fox
    Sales: 1- LAPTOP
           2- HARDDRIVE
            3- COMPUTER               thanks,
         Roseline

    Hi Roseline,
    You can adapt the solution suggested in this post Re: More than 1 records in one cell
    Thanks,
    Manish

  • Multi-Left Join Query Tuning

    I am tuning a SELECT query with 36 Left Joins in addition to normal Inner Joins and a View.
    I have used the RESULT_CACHE hint with some success.
    I have tried the LEADING hint and USE_MERGE with no success.
    Is there an Undocumented HINT and that may assist me?
    Thanks
    BRAD

    Hi, Brad,
    Welcome to the forum!
    970109 wrote:
    I am tuning a SELECT query with 36 Left Joins in addition to normal Inner Joins and a View.Why does the query need so many outer joins? Could there be a bad table design behind this problem? Post a simplified version ot the problem (with maybe 3 tables that need to be outer-joined). Post CREATE TABLE and INSERT statements for a little sample data (relevant columns only), the results you want from that sample data, and an explanation of how you get those results from that data.
    See the forum FAQ {message:id=9360002}
    For all tuning problems, see {message:id=9360003}

  • Left join query with join of three tables

    I'm trying to build a query which has me stumped. Most of the query is fairly straightforward but I've run into an issue I'm not sure how to solve.
    Background:
    We have actions stored in i_action.
    We have the available attributes for each type of action. The available attributes for each action are described in shared_action_attribute. Each type of action may have up to three attributes or none at all.
    We have the values stored for the attributes in i_attribute_value.
    A written example:
    We have a transfer action (action_code B4). The entry of the B4 action into i_action records the fact that the transfer occurred and the date on which it occurred. The available attributes for a transfer action are the receiving function code, the receiving unit number, and the transfer reason code. These available attribute types and their order are stored in shared_action_attribute. The actual values of the attributes for a specific transfer action are stored in i_attribute_value.
    Now i_action and i_attribute_value can be directly linked through action_seq in i_action and ia_action_seq in i_attribute_value. A left join built between these two tables provides results for all actions (including actions which have no attributes) and attribute values (see query 1 below).
    There are two issues. First, I only want the first two attributes. In order to specify the first two attributes, I also have to link i_attribute_value to shared_action_attribute (which is where the order is stored). I can build a simple query (without the left join) linking the three tables but then actions with no attributes would be excluded from my result set (see query 2 below).
    The second issue is that I would actually like one row returned for each action with first_attribute and second_attribute as columns instead of two rows.
    The final query will be used to create a materialized view.
    Here are the tables and examples of what's stored in them:
    TABLE i_action
    Name Type
    ACTION_SEQ NUMBER(10)
    ACTION_DATE DATE
    ACTION_CODE VARCHAR2(3)
    DELETED VARCHAR2(1)
    EXAMPLE ROWS
    ACTION_SEQ ACTION_DATE ACTION_CODE DELETED
    45765668 09-OCT-09 B2 A
    45765670 09-OCT-09 BA A
    45765672 09-OCT-09 B6 A
    45765673 09-OCT-09 B4 A
    45765674 09-OCT-09 G1 A
    45765675 09-OCT-09 M3 A
    TABLE i_attribute_value
    Name Type
    IA_ACTION_SEQ NUMBER(10)
    SACTATT_SACT_CODE VARCHAR2(3)
    SACTATT_SAT_TYPE VARCHAR2(3)
    VALUE VARCHAR2(50)
    EXAMPLE ROWS
    IA_ACTION_SEQ SACTATT_SACT_CODE SACTATT_SAT_TYPE VALUE
    45765668 B2 ACO 37B
    45765670 BA ROA D
    45765670 BA ROR P
    45765672 B6 CAT C
    45765673 B4 RFC E
    45765673 B4 TRC P
    45765673 B4 RUN 7
    45765674 G1 SS 23567
    45765674 G1 ASG W
    TABLE shared_action_attribute
    Name Type
    SACT_CODE VARCHAR2(3)
    SAT_TYPE VARCHAR2(3)
    ORDER NUMBER(2)
    TITLE VARCHAR2(60)
    EXAMPLE ROWS
    SACT_CODE SAT_TYPE ORDER TITLE
    B2 ACO 1 Office code
    BA ROR 1 Reason for reopen
    BA ROA 2 Reopen authority
    B6 CAT 1 Category
    B4 RFC 1 Receiving function code
    B4 RUN 2 Receiving unit code
    B4 TRC 3 Transfer reason code
    G1 SS 1 Staff sequence
    G1 ASG 2 Assignment reason
    QUERY 1:
    This is my current query along with its results. Most of it is straightforward select but one column is populated using the last_value analytical function (thanks to you guys). The last column in the below view stores the attribute value. What I want is to replace that single column with two columns named first_attribute and second_attribute and to eliminate any other attributes.
    SELECT ia.action_seq, ia.action_date, ia.action_code cod,
    NVL
    (LAST_VALUE (CASE
    WHEN ia.action_code = 'G1'
    AND iav.sactatt_sat_type = 'SS'
    THEN VALUE
    WHEN ia.action_code IN ('A0', 'A1')
    THEN '67089'
    END IGNORE NULLS
    ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
    ia.serial_number, ia.action_seq),
    '67089'
    ) staff_seq,
    value
    FROM i_action ia LEFT JOIN i_attribute_value iav
    ON iav.ia_action_seq = ia.action_seq
    WHERE ia.deleted = 'A';
    ACTION_SEQ ACTION_DA COD STAFF_SEQ VALUE
    45765668 09-OCT-09 B2 67089 37B
    45765670 09-OCT-09 BA 67089 D
    45765670 09-OCT-09 BA 67089 P
    45765672 09-OCT-09 B6 67089 C
    45765673 09-OCT-09 B4 67089 E
    45765673 09-OCT-09 B4 67089 P
    45765673 09-OCT-09 B4 67089 7
    45765674 09-OCT-09 G1 23567 23567
    45765674 09-OCT-09 G1 23567 W
    45765675 09-OCT-09 M3 23567
    QUERY 2:
    This query limits to the first two attributes but it also drops actions which have no attributes and it still creates multiple rows for each action instead of a single row with two columns for the attributes.
    SELECT ia.action_seq, ia.action_date, ia.action_code cod,
    NVL
    (LAST_VALUE (CASE
    WHEN ia.action_code = 'G1'
    AND iav.sactatt_sat_type = 'SS'
    THEN VALUE
    WHEN ia.action_code IN ('A0', 'A1')
    THEN '67089'
    END IGNORE NULLS
    ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
    ia.serial_number, ia.action_seq),
    '67089'
    ) staff_seq,
    value
    FROM shared_action_attribute saa, ims_action ia, ims_attribute_value iav
    WHERE iav.ia_action_seq = ia.action_seq
    AND iav.sactatt_sact_code = saa.sact_code
    AND iav.sactatt_sat_type = saa.sat_type
    AND saa.display_order IN ('1','2')
    AND ia.deleted = 'A';
    ACTION_SEQ ACTION_DA COD VALUE
    45765668 09-OCT-09 B2 67089 37B
    45765670 09-OCT-09 BA 67089 D
    45765670 09-OCT-09 BA 67089 P
    45765672 09-OCT-09 B6 67089 C
    45765673 09-OCT-09 B4 67089 E
    45765673 09-OCT-09 B4 67089 7
    45765674 09-OCT-09 G1 23567 23567
    45765674 09-OCT-09 G1 23567 W
    I found this pretty complex to try to write out - I hope I've been clear.
    Thanks so much!

    Ok, here's more information with a simplified question. I figured out the syntax for building my query with the three tables. My final query returns multiple rows (multiple attributes per action). Instead of multiple rows, I'd like two columns (first_attribute, and second_attribute) in a single row (I only need the first two attributes).
    Here's the action table:
    CREATE TABLE I_ACTION
      ACTION_SEQ               NUMBER(10)           NOT NULL,
      ACTION_DATE              DATE,
      ACTION_CODE              VARCHAR2(3 BYTE)     NOT NULL,
      DELETED                  VARCHAR2(1 BYTE),
    );With the following rows added:
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765668, '09-oct-2009', 'B2', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765670, '09-oct-2009', 'BA', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765672, '09-oct-2009', 'B6', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765673, '09-oct-2009', 'B4', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765674, '09-oct-2009', 'G1', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765675, '09-oct-2009', 'M3', 'A');
    COMMIT;The attribute table is:
    CREATE TABLE I_ATTRIBUTE_VALUE
      IA_ACTION_SEQ          NUMBER(10)             NOT NULL,
      SACTATT_SACT_CODE      VARCHAR2(3 BYTE)       NOT NULL,
      SACTATT_SAT_TYPE       VARCHAR2(3 BYTE)       NOT NULL,
      VALUE                  VARCHAR2(50 BYTE),
    );With the following rows:
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765668, 'B2', 'ACO', '37B');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765670, 'BA', 'ROR', 'P');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765670, 'BA', 'ROA', 'D');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765672, 'B6', 'CAT', 'C');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765673, 'B4', 'RFC', 'E');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765673, 'B4', 'RUN', '7');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765673, 'B4', 'TRC', 'P');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765674, 'G1', 'SS', '23567');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765674, 'G1', 'ASG', 'W');
    COMMIT;And finally, the shared table:
    CREATE TABLE SHARED_ACTION_ATTRIBUTE
      SACT_CODE      VARCHAR2(3 BYTE)               NOT NULL,
      SAT_TYPE       VARCHAR2(3 BYTE)               NOT NULL,
      TITLE          VARCHAR2(25 BYTE)              NOT NULL,
      DISPLAY_ORDER  NUMBER(2)                      NOT NULL
    );With the following rows:
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B4', 'RFC', 'Y', 'Rcv. Function Code', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B6', 'CAT', 'Y', 'Category', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('G1', 'SS', 'Y', 'Staff Name', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B2', 'ACO', 'Y', '"Other" Office Code', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B4', 'RUN', 'Y', 'Receiving Unit Number', 2);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B6', 'LEP', 'N', 'LEP Issue/Sub Category', 2);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B4', 'TRC', 'Y', 'Transfer Reason Code', 3);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B6', 'NEP', 'N', 'NEP Issue', 3);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('G1', 'ASG', 'Y', 'Assignment Reason', 2);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B2', 'MSN', 'S', 'Machine Serial Number', 3);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('BA', 'ROR', 'Y', 'Reopen Reason', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('BA', 'ROA', 'Y', 'Reopen Authority', 2);
    COMMIT;Now, this is my current query (it's changed from my first post):
    SELECT ia.action_seq, ia.ici_charge_inquiry_seq, ia.action_date,
           ia.serial_number, ia.reporting_office, ia.reporting_function,
           ia.reporting_unit, ia.action_code, ia.machine_serial_number,
           NVL
              (LAST_VALUE (CASE
                              WHEN ia.action_code = 'G1'
                                 THEN VALUE
                              WHEN ia.action_code IN ('A0', 'A1')
                                 THEN '67089'
                           END IGNORE NULLS
                          ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
                ia.serial_number, ia.action_seq),
               '67089'
              ) staff_seq,
           (CASE
              WHEN display_order = '1'
              THEN VALUE
           END) first_attribute,
           (CASE
              WHEN display_order = '2'
              THEN VALUE
           END) second_attribute
      FROM ims_action ia
      LEFT JOIN ims_attribute_value iav
           ON iav.ia_action_seq = ia.action_seq
      LEFT JOIN shared_action_attribute
           ON sactatt_sact_code = sact_code
         AND sactatt_sat_type = sat_type
    WHERE ia.deleted = 'A';Which gives me the following results:
    ACTION_SEQ ACTION_DA ACT STAFF_SEQ FIRST_ATTRIBUTE SECOND_ATTRIBUTE   
      45765668 09-OCT-09 B2  67089     37B                                
      45765670 09-OCT-09 BA  67089                     D                  
      45765670 09-OCT-09 BA  67089     P                                  
      45765672 09-OCT-09 B6  67089     C                                  
      45765673 09-OCT-09 B4  67089     E                                  
      45765673 09-OCT-09 B4  67089                     7                  
      45765673 09-OCT-09 B4  67089                                        
      45765674 09-OCT-09 G1  23567                     W                  
      45765674 09-OCT-09 G1  23567     23567                              
      45765675 09-OCT-09 M3  23567                                       The result I WANT is similar but I want the two separate attribute columns on one row as such:
    ACTION_SEQ ACTION_DA ACT STAFF_SEQ FIRST_ATTRIBUTE SECOND_ATTRIBUTE   
      45765668 09-OCT-09 B2  67089     37B                                
      45765670 09-OCT-09 BA  67089     P               D                  
      45765672 09-OCT-09 B6  67089     C                                  
      45765673 09-OCT-09 B4  67089     E               7                  
      45765674 09-OCT-09 G1  23567     23567           W                  
      45765675 09-OCT-09 M3  23567                          Thanks so much!

  • Left Join query help

    select * from ort
    bid mid
    18083 7
    select * from st
    tid bid mid act
    318 18083 5 20091
    318 18083 6 20091
    321 18083 7 NULL
    318 18083 16 23970
    my out put should be
    318 18083 6 20091
    (basic idea is
    In order to do this I wrote the following query. But I am getting the following error ora-01799. How do i fix this?
    select ort.bid,st.tid from ort
    left join st
    on ort.BiD = st.bid
    and st.mid in
    (select max(MID)
    from St
    where BID = ort.BID and TID is not null and MID <= ort.MID
    and ACT is not null
    )

    May be this will clear up what I am trying to acheive a little better. I truly appreciate all your help. I have enclosed column headings and data items within double quotes and data is enclosd in double-quotes and separated by comma.
    SQL> desc ort
    "Name" "Null?" "Type"
    "BID" "NUMBER"
    "MID" "NUMBER"
    SQL> desc st
    "Name" "Null?" "Type"
    "TID" "NUMBER"
    "BID" "NUMBER"
    "MID" "NUMBER"
    "ACT" "NUMBER"
    "LTP" "NUMBER(10)"
    SQL> select * from ort
    2 ;
    "BID" "MID"
    "18083", "7"
    "18083", "6"
    "18083", "16"
    "18083", "277"
    "18083", "117"
    SQL> select * from st;
    "TID" "BID" "MID" "ACT" "LTP"
    "NULL", "18083", "117", "NULL", "246"
    "NULL", "18083", "277", "NULL", "246"
    "246", "18083", "272", "54998", "246"
    "318", "18083", "6", "20091" "NULL"
    "321", "18083", "7", "NULL", "NULL"
    "318", "18083", "16", "23970", "NULL"
    6 rows selected.
    SQL> SELECT ort.bid, st.tid, st.mid, st.act
    2 FROM ort
    3 LEFT OUTER JOIN
    4 st
    5 ON ort.bid = st.bid
    6 AND st.mid IN (SELECT mid
    7 FROM myortview);
    "BID" "TID" "MID" "ACT"
    "18083", "246", "272", "54998"
    "18083", "246", "272", "54998"
    "18083", "246", "272", "54998"
    "18083", "246", "272", "54998"
    "18083", "246", "272", "54998"
    SQL> -- expected result is
    bid tpid ort.mid st. mid
    "18083", "246", "277", "272"
    "18083", "246", "117", "116"
    "18083", "318", "16", "16"
    "18083", "318", "6", "6"
    "18083", "318", "7", "6"

  • Left join query prob

    Hi,
    I have one ejbql for selecting records from one table which are not in the other table.There is actually records in the data base but by using this query it is retrieving noting . The corresponding pl/sql query is working fine and it is retrieve correct result .what is wrong with my query.. Plz help me to correct this query...
    My ejbql is below...
    EJB-QL
    SELECT C.name FROM person C LEFT JOIN C.address FC WHERE C.id.name= FC.id.name AND FC.id.pcode IS NULL
    There is no result while executing this query. The correspoinding pl sql query is
    PL/SQL
    SELECT e_person .* FROM e_person ep LEFT JOIN e_address ea ON ep .name= ea .name WHERE ea.pcode IS NULL
    plz help ...
    Thanks in advance
    Ani

    Enable logging and include the SQL generated for the JPQL.
    JPQL will also join via the primary/foreign key define in the mapping when you do C.address, if this is not name then you may be joining by something else. You could just declare the Address independent of the Employee if you do not wish to join by primary key (although this seems odd).
    -- James : http://www.eclipselink.org

  • Left Join Query Question

    Version 10.2.0.4.0
    I have a question on the expected behavior of the query below.
    When I run the query below with the constraint on t1.partid = 789,  I get the query result t2.Indicator showing "SPECIAL" as expected.
    However, if I remove the constraint, and return all orders and parts, for the "789" part, the Indicator column is null.
    select t1.orderid, t1.partid, t2.Indicator
    from Orders a left outer join
    select partid, 'SPECIAL' as Indicator
    from vendors
    where vendorname like '%ABC%'
    ) t2
    on t1.partid = t2.partid
    where t1.partid = '789'
    I can address the issue with a case statement (below) or likely restructuring into a better statement. 
    But I'm just curious if this is expected or if there is some SQL rule being violated in the first example.
    I tried to search for this to see if it was already addressed but didn't have much luck.
    This works:
    select t1.orderid, t1.partid,
    case when t1.partid is not null then "SPECIAL" else null end as Indicator
    from Orders a left outer join
    select partid, 'SPECIAL' as Indicator
    from vendors
    where vendorname like '%ABC%'
    ) t2
    on t1.partid = t2.partid

    Sorry, it's been a while since I posted and should have read the rules.  And I didn't properly reference the alias.  So Post #1 was bad. 
    When I mockup a small set of data (shown below), I don't get the error.  The original query actually joins to a few other (seemingly) irrelevant tables and I tried to simplify it here  So I guess if I can't replicate it, then there might not be much assistance that can be provided.
    This was more of a curiousity than anything else to see if perhaps someone came across this before.
    For what it's worth:
    create table t1 (orderid number,
                 partid varchar2(20)
    create table t2
            (vendorid varchar2(20),
             partid varchar2(20)
    insert into t1 values(1, '123');
    insert into t1 values(2, '456');
    insert into t1 values(3, '789');
    insert into t2 values ('ABC','789');
    insert into t2 values ('DEF','123');
    insert into t2 values ('EFG','456');
    insert into t2 values ('ABC','7891');
    insert into t2 values ('DEF','1231');
    insert into t2 values ('EFG','4561');
    select t1.orderid, t1.partid, t2.Indicator
    from t1 left outer join
    select partid, 'SPECIAL' as Indicator
    from t2
    where vendorid like '%ABC%'
    ) t2
    on t1.partid = t2.partid
    the query that gives unexpected behavior is (although I can't replicate on this simplified version):
    select t1.orderid, t1.partid, t2.Indicator
    from t1 left outer join
    select partid, 'SPECIAL' as Indicator
    from t2
    where vendorid like '%ABC%'
    ) t2
    on t1.partid = t2.partid

  • Adding a field to rhe join query

    Dear Friends,
    I need your help in adding a field to a join query.
    I have created a reort using join(SQVI). Now i want to add an addition field to that report. The issue is the field i am looking at is stored in structure. Is there any way i can add this field....
    Sanjay

    It is not possible here (in SQVI).
    you have to Go with Query.
    SQ02 using this Create infoset, add all the tables , here include that structure field, and then add the logic to populate the field.
    Now go to SQ01 and create Query.

  • How to generate a query involving multiple tables(one left join others)

    Hi, all,
    I want to query a db like these:
    I need all the demographics information(from table demo) and their acr info(from table acr), and their clinical info(from table clinical), and their lab info(from table lab).
    The db is like this:
    demo->acr: one to many
    demo->clinical info: one to many
    demo->lab info: one to many
    I want to get one query result which are demo left join acr, and demo left join clinical, and demo left join lab. I hope the result is a record including demo info, acr info, clinical info, and lab info.
    How could I do this in SQL?
    Thanks a lot!
    Qian

    Thank you very, very much!
    Actually, I need a huge query to include all the tables in our db.
    We are running a clinical db which collects the patients demographics info, clinical info, lab info, and many other information.
    The Demographics table is a center hub which connects other tables. This is the main architecture.
    My boss needed a huge query to include all the information, so others could find what they need by filtering.
    As you have found, because one patients usually has multiple clinical/lab info sets, so the result will be multiplied! the number of result=n*m*k*...
    My first plan is to set time point criteria to narrow all the records with one study year. If somebody needs to compare them, then I have to show them all.
    So I have to know the SQL to generate a huge query including as many tables as possible.
    I show some details here:
    CREATE TABLE "IMMUNODATA"."DEMOGRAPHICS" (
    "SUBJECTID" INTEGER NOT NULL,
    "WORKID" INTEGER,
    "OMRFHISTORYNUMBER" INTEGER,
    "OTHERID" INTEGER,
    "BARCODE" INTEGER,
    "GENDER" VARCHAR2(1),
    "DOB" DATE,
    "RACEAI" INTEGER,
    "RACECAUCASIAN" INTEGER,
    "RACEAA" INTEGER,
    "RACEASIAN" INTEGER,
    "RACEPAC" INTEGER,
    "RACEHIS" INTEGER,
    "RACEOTHER" VARCHAR2(50),
    "SSN" VARCHAR2(11),
    PRIMARY KEY("SUBJECTID") VALIDATE
    CREATE TABLE "IMMUNODATA"."ACR" (
    "ID" INTEGER NOT NULL,
    "THEDATE" DATE ,
    "SUBJECTID" INTEGER NOT NULL,
    "ACR_PAGENOTCOMPLETED" VARCHAR2(1000) ,
    "ACR_MALARRASHTODAY" INTEGER ,
    "ACR_MALARRASHEVER" INTEGER ,
    "ACR_MALARRSHEARLIESTDATE" DATE ,
    PRIMARY KEY("ID") VALIDATE,
    FOREIGN KEY("SUBJECTID") REFERENCES "IMMUNODATA"."DEMOGRAPHICS" ("SUBJECTID") VALIDATE
    CREATE TABLE "IMMUNODATA"."CLIN" (
    "ID" INTEGER NOT NULL,
    "THEDATE" DATE ,
    "SUBJECTID" INTEGER NOT NULL,
    "CLIN_PAGENOTCOMPLETED" VARCHAR2(1000) ,
    "CLIN_FATIGUE" VARCHAR2(20) ,
    "CLIN_FATIGUEDATE" DATE ,
    "CLIN_FEVER" VARCHAR2(20) ,
    "CLIN_FEVERDATE" DATE ,
    "CLIN_WEIGHTLOSS" VARCHAR2(20) ,
    "CLIN_WEIGHTLOSSDATE" DATE ,
    "CLIN_CARDIOMEGALY" VARCHAR2(20) ,
    PRIMARY KEY("ID") VALIDATE,
    FOREIGN KEY("SUBJECTID") REFERENCES "IMMUNODATA"."DEMOGRAPHICS" ("SUBJECTID") VALIDATE
    Other tables are alike.
    Thank very much!
    Qian

  • Filter on "---" in Left Outer Join Query

    Hi guys,
    very basic question but I have not found an answer, yet. I built an left outer join MDO query and need to select all those dataset that did not find a "partner", that is fields are set to "---". However, I do not manage to filter on '---', NULL...
    When using SQL, it appears to be a String "---" but in MDO???

    Hey guys, found out that IS NULL needs to be used in query. Anyway, it appears to cause problems somewhere else.
    I placed an IS NULL filter expression inside a join query but query did not return any results when called in by transaction. After removing this line, both NULL and NOT NULL data were returned. When used in test mode, IS NULL was working fine...

  • Left outer join query

    Hi Experts,
        I am facing a problem with left outer join query. Am using one standard table and ztable for this join. My problem is values are not extracted from the Ztable.
    Query:
          SELECT  b~lifnr b~belnr b~gjahr b~xblnr b~shkzg b~blart b~zfbdt b~budat b~wrbtr
             b~wskto b~zlspr s~EXTRACT_STATUS s~maturity_date FROM bsik AS b
             LEFT OUTER JOIN zprm_rvne_sapdoc AS s
             ON s~belnr  EQ  b~belnr
             AND s~gjahr EQ b~gjahr
             INTO CORRESPONDING FIELDS OF TABLE it_join
                WHERE b~zlsch = p_zlsch
                AND b~xblnr IN so_invno
                ORDER BY b~lifnr b~xblnr.
    I have all entries of BSIK table in Ztable with extract status as Y but this query is not fetching extract status and maturity date of ztable so it is blank in the internal table.
    Need solution.
    Regards
    Sridevi S

    Hi,
    see the sample wiki for writing the Left outer join
    http://wiki.sdn.sap.com/wiki/display/Snippets/EmployeeInfotype0000to9999ChangeHistory
    Specifying Two or More Database Tables as a Left Outer Join
    The left outer join, on the other hand, reads lines from the left-hand database table or join even if there is no corresponding line in the right-hand table.
    SELECT...
      FROM <tab> LEFT [OUTER] JOIN <dbtab> [AS <alias>] ON <cond>
           <options>
    <tab> and <dbtab> are subject to the same rules and conditions as in an inner join. The OUTER addition is optional. The tables are linked in the same way as the inner join with the one exception that all lines selected from <tab> are included in the final selection. If <dbtab> does not contain any lines that meet the condition <cond>, the system includes a single line in the selection whose columns from <dbtab> are filled with null values.
    In the left outer join, more restrictions apply to the condition <cond> than in the inner join. In addition to the above restrictions:
    EQ or = is the only permitted relational operator.
    There must be at least one comparison between columns from <tab> and <dbtab>.
    The WHERE clause may not contain any comparisons with columns from <dbtab>. All comparisons using columns from <dbtab> must appear in the condition <cond>.
    If we have two tables named stud1,stud2 with the following data
    Stud1: id Name stud2: id Name
    1 xxx 1 aaa
    2 yyy 2 bbb
    3 zzz 4 ccc
    4 www 6 ddd
    When we use Left Outer Join we get the output as:
    1 aaa
    2 bbb
    3 <Null>
    4 ccc
    When we use Right Outer Join we get the output as:
    1 aaa
    2 bbb
    4 ccc
    <Null> ddd
    When we use Full Outer Join we get the output as:
    1 aaa
    2 bbb
    3 <Null>
    4 ccc
    <Null> ddd
    Prabhudas

  • JPA OnetoMany  QUERY LEFT JOIN BUG

    Using JPA in JDev 10.1.3.1.0.3984
    Database: Firebird 1.51LI-V1.5.3.4870 Firebird 1.5/tcp
    Driver: Jaybird JCA/JDBC driver Version: 2.1
    TopLink, version: Oracle TopLink Essentials - 2006.8 (Build 060829)
    If I use normal JOIN it works.
    On LEFT JOIN I get a {oj [/b] before the table name and a [b]} at the end.
    public class Cliente{
        @OneToMany(mappedBy = "cliente")
        @JoinColumn(name = "CDCLIENTE", referencedColumnName = "CDCLIENTEREQUISITANTE")
        private List<Requisicao> requisicoes;
    public class Requisicao
        @ManyToOne
        @JoinColumn(name = "CDCLIENTEREQUISITANTE", referencedColumnName = "CDCLIENTE")
        private Cliente cliente;
    EntityManager em = getEntityManager();
    String sql = "SELECT c FROM Cliente c LEFT JOIN c.requisicoes req";
    Query q = em.createQuery(sql);
    List rs = q.getResultList();Result SQL:
    SELECT DISTINCT t0. <OMITTED> FROM {oj [/b]CLIENTE t0 LEFT OUTER JOIN REQUISICAO t1 ON (t1.CDCLIENTEREQUISITANTE = t0.CDCLIENTE)[b]}

    You cannot define an ON clause with Criteria, nor JPQL.
    Perhaps you can reword the query to avoid needing an ON clause.
    What is the query you want to do (in english)?
    Can you just use an OR in the where clause?
    There is a enhancement request to have ON clause support added, please vote for it.
    https://bugs.eclipse.org/bugs/show_bug.cgi?id=312146
    James : http://www.eclipselink.org

  • Query Help (Left Join?)

    Hi all
    Im trying to write a query and am having problems with the way that the data is displayed.
    My issue is essentially with one column EMPL_STAT_CD.
    Within this column contains diffrent words standing for diffrent things such as Table, Lamp etc.
    With my query i am specifically looking for everyone that has bought a Table or a table AND lamp.
    How i want the data to be displayed is
    table Blank
    table Blank
    Table Blank
    Table Lamp
    Table Blank
    Table Lamp
    Im pretty sure i can do a left outer join to display all the data in the left table, but am having problems with displaying the right table with the blanks.
    Current Query
    SELECT
    DISTINCT(A.PRSN_INTN_ID),
    B.FRST_NM AS FIRST_NAME,
    B.MID_NM AS MIDDLE_NAME,
    B.LAST_NM AS LASTNAME,
    B.BRTH_DT AS BIRTH_DATE,
    A.EMPL_CAT_ID AS EMPLOYMENT_ID,
    A.EE_EMPLCAT_EFBEGDT AS BEGIN_DATE,
    C.ADDR_LINE_1_TX AS ADDRESS,
    C.CITY_NM AS CITY,
    C.ST_CD AS PROVINCE,
    C.ZIP_CD AS POSTAL_CODE,
    C.CTRY_CD AS COUNTRY_CODE,
    B.PRIM_LANG_CD AS LANGUAGE,
    D.DLVR_GRSS_AT AS PENSION_AMOUNT,
    FROM
    PRSN B,
    PRSN_ADDR C,
    PRSN_TXDS_DLVR D,
    PRSN_DBPMTINST E,
    EE_EMPL_CAT A
    WHERE A.PRSN_INTN_ID = B.PRSN_INTN_ID AND
    A.PRSN_INTN_ID = C.PRSN_INTN_ID AND
    A.PRSN_INTN_ID = D.PRSN_INTN_ID AND
    A.PRSN_INTN_ID = E.PRSN_INTN_ID AND
    D.PMT_ID = E.PMT_ID AND
    D.PMT_EFDT = '2008-03-01'
    AND E.DB_PMTINST_STAT_CD = 'A'
    AND E.PMT_INST_EFENDDT = '2299-12-31'
    AND E.PMT_ADJ_CD NOT IN ('R', 'A')
    AND E.PMT_ID IN(1000,6000,2510,2520)
    AND B.CURR_RSLT_CD = 'Y'
    AND A.EMPL_CAT_ID IN(Table,Lamp)
    AND A.EE_EMPLCAT_EFENDDT='2299-12-31'
    AND C.ADDR_ID = 10
    AND C.PRSN_ADDR_EFENDDT = '2299-12-31'
    AND C.CURR_RSLT_CD = 'Y'
    Any input greatly appreciated

    You don't have a query problem you have a design problem that goes back to database basics.
    Never, ever, store multiple values in a single column.
    Fix your design and the problem goes away.
    http://www.psoug.org/reference/normalization.html

  • Query problem, two working queries how to left join together?

    Hello,
    I have a two queries that I have been trying to put together for a couple days. I'm frazzled. Hopefully you can help.
    The first query returns all rows from the database. The second query returns only one row (because the way it is currentlly set up in the Where clause). So I know that will have to change. For each row returned in Query1, I need the two fields from Query2 included
    (so the link would be through client.Id (which is an indexed field) or client.Accountnumber?
    This query, returns all records in the database:
    Select client.Id, client.accountnumber, client.Namelast,
    dmlocation.city ||', '||dmlocation.state as CityState,
    client.salesTerritory_client ||'-'|| dmuser.namefirst_user ||' '|| dmuser.namelast_user as Territory,
    MaxDates.LastRun, client.creditrisk, client.customercategory
    from client
    Left join fctclientcoverage on fctclientcoverage.client_id = client.id
    Left join dmlocation on fctclientcoverage.location_id = dmlocation.id
    Left join dmuser on dmuser.id = client.id
    Left join (Select to_char(Max(dmdate.calendardate),'MM/DD/YY') as LastRun, Client.Id
    from dmdate, client, fctadorder
    where dmdate.id = fctadorder.lastinsert_date_id and client.id = fctadorder.primaryorderer_client_id
    group by client.id) MaxDates ON client.id = MaxDates.Id
    where(fctclientcoverage.Ccoverrecordstopdate Is Null)
    Order by client.namelast;
    Query 2, only returns 1 row, so for each row returned above, the two fields selected in this query should accompany each row. But how to link these two selects using the client.accountnumber (or perhaps by dmcliet.id)?
    Select booked.CurRev, booked.LastRev from (
    Select (sum(Case When dmDate.CalendarDate &gt;= '29-DEC-2008' and dmDate.CalendarDate &lt; '
    Then fctinsertchargedetail.Amount_insertDetail Else 0 End)) As CurRev,
    (sum(Case When dmDate.CalendarDate &gt;= '29-DEC-2007' and dmDate.CalendarDate &lt; '
    Then fctinsertchargedetail.Amount_insertDetail Else 0 End)) As LastRev
    from fctAdorder
    Inner Join client On fctAdorder.primaryorderer_client_id = client.id
    Inner Join fctinsertion On fctAdorder.id=fctinsertion.fctAdorder_id
    Inner Join fctinsertchargesummary On fctinsertion.id=fctinsertchargesummary.insertion_id
    Inner Join dmDate On fctinsertion.insert_date_id=dmDate.id
    Inner Join fctinsertchargedetail On fctinsertchargesummary.id=fctinsertchargedetail.insertchargesummary_id
    WHERE client.accountnumber = '12345678' and
    dmDate.CalendarDate &gt;= '29-DEC-2007' And dmDate.CalendarDate &lt; ') booked;
    Thanks for your time.

    Yes, You are correct!
    I just recently got the query working with the aid of another forum.
    The sad part is, all though the first query took 11 seconds to return 180,000 rows (thats good); The second query took 4 minutes to calculate and return all it's rows (that's bad). Together the query ran for over 4 minutes. Way too slow.
    Being brand new to oracle I have to try and figure away to cut this time down. Perhaps I'm not considering something?
    I orginally brought into my .net app the results from the first query and then in the rowdatabound event I queried each row to get the information needed from the second query. That was way too slow also. It was recommended to try and return all needed data at once.
    I've been given a task to emulate a current application, (which I do not have access to it's code), that brings back all of this same information that I am using. It only takes them maybe 15 seconds to run, to bring back all. Of course they were experienced oracle sql developers.
    So I guess my next step is to try and improve that second query. Thanks for replying to this Frank. I'll be back. Are you or is anyone good at knowing how to optimzie queries? I'm reading a book now trying out suggestions. Nothing is working yet.
    thanks

  • Issue on left outer join query in 10G

    Dear Mr./Mrs./Ms. Oracle expertise,
    The following query structure can function properly in Oracle 9i, but not work in Oracle 10G.
    Please kindly advise what happened in Oracle 10G?
    Is it a bug in 10G? If yes, any patch for it?
    select a.col1,
    a.col2,
    a.col5,
    b.col3,
    b.col4
    from t1 a
    inner join t2 c on c.col1 = a.col1
    left join (select col3
    col4
    from t3) b
    on b.col1 = a.col1
    and a.col2 = 'Y' <---------- seems 10G cannot allow this condition
    where a.col5 = 123;
    Purpose of above SQL
    ====================
    Only those records from t1 table with a.col2 = 'Y' will
    do the left outer join to "b" table and get the columns b.col3 and b.col4
    Result set in Oracle 9i
    =======================
    col1 col2 col5 col3 col4
    AAA N 111
    BBB N 222
    CCC Y 333 XYZ OOO
    DDD Y 444 QPR 111
    Result set in Oracle 10G
    ========================
    col1 col2 col5 col3 col4
    AAA N 111
    BBB N 222
    CCC Y 333
    DDD Y 444
    Rgds,
    Ken Chan

    Dear Schneider,
    Sorry that there was a typo in my query.
    Below is the revised query.
    select a.col1,
    a.col2,
    a.col5,
    b.col3,
    b.col4
    from t1 a
    inner join t2 c on c.col1 = a.col1
    left join (select col1,
    col3,
    col4
    from t3) b
    on b.col1 = a.col1
    and a.col2 = 'Y' <---------- seems 10G cannot allow this condition
    where a.col5 = 123;
    Thanks.
    Rgds,
    ken Chan

Maybe you are looking for

  • Email of the administrator in STS

    Hi all, Is there anyplace in STS to indicate the email address of the person who sends the email? I am doing some proofs with STS but the emails don´t send and I am looking if it can be for this. Thanks

  • Questions about SES

    1. How many files SES can crawl? About 500,000 ??? 2. What are the System Requirements? 3. Can SES crawl Lotus Notes Data? 4. What file types can be crawled? How many different?! 5. Free or costs for using SES in enterprises? THX

  • Setting backup on two different locations

    Hello, people. I am a newbie DBA and at my work place , Using RMAN for my backup strategy I want to set up a backup to two different location ( e.g A:\backup and B:\backup), I have configure two channels, and also set configure device type disk paral

  • JavaScript Date object isssue

    Am I crazy or this does not work correctly in Safari 6.1.1 (7537.73.11), on Mac OS X Lion 10.7.5 (11G63). function myFunction() var date1 = new Date(2014, 2, 30, 1, 59); var date2 = new Date(date1.getTime() + 120 * 1000); //add 120 seconds alert(date

  • Running reports during certain timeframes only?

    In our environment, custom reports have been created which, when run, potentially have the ability to bring the system to its knees.  I would like to set up some type of authorization that allows the running of reports only at a specific times each d