My Confusion of outer join
Oracle 10gI 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');
BenPublished 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.
Tags: Database
Similar Questions
-
Hello
Can someone tell me please the difference between these two motions.
Select *.
from T1, T2
where T1. C1 = T2. C1 (+)
and T1. C2 = 'Y ';
Select *.
from T1, T2
where T1. C1 = T2. C1 (+)
and T1. C2(+) = « Y » ;
I'm just confused why add an outer join in case of checking the values of constant?
I know that the concept of outer join is make no corresponding data on the part of the table. But when external associate a constant value which corresponds to the average?
Please specify.
And one more thing... If I want to check two values, how can I write the query below correctly?
Select *.
from T1, T2
where T1. C1 = T2. C1 (+)
and T1. C2 (+) ('y', 'NA');
Thank you
VInodhttp://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:897774300346615341 #901884400346525375
-
Bug with an outer join, or &; Analytics function (or rownum)
Hello
Seems to be a combination of an outer join, OR and rownum confuses the CBO.
First request is without rownum, the second is with rownum.
The second query expects 203 t lines and never ends. It should behave the same as query 1, with 24 M lines.
Remove the GOLD clause query 2 allows him to behave as a query 1, with 24 M lines.
We never saw it? Is there a solution?
SELECT * FROM message i LEFT JOIN (SELECT hi.message_id, hi.update_dt FROM message_hist hi) h ON (t.id = h.master_id AND(t.update_dt = h.update_dt OR h.update_dt <TO_DATE('150901','RRMMDD'))); ----------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 24M| 13G| 475G (2)|999:59:59 | | 1 | NESTED LOOPS OUTER | | 24M| 13G| 475G (2)|999:59:59 | | 2 | TABLE ACCESS FULL | MESSAGE | 8037K| 1318M| 29883 (2)| 00:06:59 | | 3 | VIEW | | 3 | 1302 | 59136 (2)| 00:13:48 | |* 4 | TABLE ACCESS FULL| MESSAGE_HIST | 3 | 168 | 59136 (2)| 00:13:48 | ----------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - filter("I"."MESSAGE_ID"="HI"."MESSAGE_ID" AND ("HI"."UPDATE_DT"<TO_DATE('150901','RRMMDD') OR "I"."UPDATE_DT"="HI"."UPDATE_DT")) ---------------- SELECT * FROM message i LEFT JOIN (SELECT hi.message_id, hi.update_dt , ROWNUM FROM message_hist hi) h ON (t.id = h.master_id AND(t.update_dt = h.update_dt OR h.update_dt <TO_DATE('150901','RRMMDD'))); ------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 203T| 112P| 475G (2)|999:59:59 | | 1 | NESTED LOOPS OUTER | | 203T| 112P| 475G (2)|999:59:59 | | 2 | TABLE ACCESS FULL | MESSAGE | 8037K| 1318M| 29883 (2)| 00:06:59 | | 3 | VIEW | | 25M| 10G| 59151 (2)| 00:13:49 | |* 4 | VIEW | | 25M| 10G| 59151 (2)| 00:13:49 | | 5 | COUNT | | | | | | | 6 | TABLE ACCESS FULL| MESSAGE_HIST | 25M| 1355M| 59151 (2)| 00:13:49 | ------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - filter("I"."MESSAGE_ID"="H"."MESSAGE_ID" AND ("I"."UPDATE_DT"="H"."UPDATE_DT" OR "H"."UPDATE_DT"<TO_DATE('150901','RRMMDD')))
RowNum in a subquery is supposed to ensure that the subquery is evaluated completely before filtering, otherwise, how could you go out rownum?
Your question is compounded because of the join condition that forces a level of nested loops, which means that the table should be fully analysed once for each line of conduct rowsource. You can either transform the join in an equijoin and allow a hash to run, or you join could materialize the subquery once.
Allow the hash join:
SELECT count (*)
Message FROM
LEFT JOIN (SELECT hi.message_id, hi.update_dt
ROWNUM
OF message_hist salvation) PM ON (i.message_id = h.message_id
AND i.update_dt = h.update_dt)
LEFT JOIN (SELECT hi.message_id, hi.update_dt
ROWNUM
OF message_hist salvation) h2 ON (i.message_id = h2.message_id
AND h2.update_dt<>
AND h2.update_dt <> i.update_dt)
/
----------------------------------------------------------------------------------------
| ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1. 66. 211 (1) | 00:00:01 |
| 1. GLOBAL TRI | | 1. 66. | |
|* 2 | EXTERNAL RIGHT HASH JOIN | | 800 | 52800 | 211 (1) | 00:00:01 |
|* 3 | VIEW | | 1. 22. 70 (0) | 00:00:01 |
| 4. COUNTY | | | | | |
| 5. TABLE ACCESS FULL | MESSAGE_HIST | 1. 22. 70 (0) | 00:00:01 |
|* 6 | EXTERNAL RIGHT HASH JOIN | | 800 | 35200. 141 (1) | 00:00:01 |
| 7. VIEW | | 1. 22. 70 (0) | 00:00:01 |
| 8. COUNTY | | | | | |
| 9. TABLE ACCESS FULL | MESSAGE_HIST | 1. 22. 70 (0) | 00:00:01 |
| 10. TABLE ACCESS FULL | MESSAGE | 800 | 17600 | 70 (0) | 00:00:01 |
----------------------------------------------------------------------------------------
Information of predicates (identified by the operation identity card):
---------------------------------------------------
2 - access("I".") MESSAGE_ID '= 'H2'.' MESSAGE_ID "(+))"
filter ("H2". "UPDATE_DT" (+)<>'I'. " ("' UPDATE_DT")
3 - filter("H2".") UPDATE_DT "(+)<>
6 - access("I".") "UPDATE_DT" ="H" UPDATE_DT "(+) AND"
"I"." ' MESSAGE_ID ' ="H" MESSAGE_ID "(+))"
Materialize the subquery:
WITH h AS (SELECT / * + MATERIALIZE * / hi.message_id, hi.update_dt)
ROWNUM
OF message_hist salvation)
SELECT count (*)
Message FROM
LEFT JOIN: ON (i.message_id = h.message_id
AND (i.update_dt = h.update_dt OR h.update_dt<>
----------------------------------------------------------------------------------------------------------
| ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
----------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1. 22. 1740 (0) | 00:00:01 |
| 1. TRANSFORMATION OF THE TEMPORARY TABLE. | | | | |
| 2. LOAD SELECT ACE | SYS_TEMP_0FD9D6810_5B8F6E67 | | | | |
| 3. COUNT | | | | | |
| 4. TABLE ACCESS FULL | MESSAGE_HIST | 1. 22. 70 (0) | 00:00:01 |
| 5. GLOBAL TRI | | 1. 22. | |
| 6. NESTED EXTERNAL LOOPS | | 800 | 17600 | 1670 (0) | 00:00:01 |
| 7. TABLE ACCESS FULL | MESSAGE | 800 | 17600 | 70 (0) | 00:00:01 |
| 8. VIEW | | 1. | 2 (0) | 00:00:01 |
|* 9 | VIEW | | 1. 22. 2 (0) | 00:00:01 |
| 10. TABLE ACCESS FULL | SYS_TEMP_0FD9D6810_5B8F6E67 | 1. 22. 2 (0) | 00:00:01 |
----------------------------------------------------------------------------------------------------------
Information of predicates (identified by the operation identity card):
---------------------------------------------------
9 - filter("I".") ' MESSAGE_ID ' ="H" MESSAGE_ID' AND ('I'. "" "UPDATE_DT"="H" UPDATE_DT' OR
"H"." UPDATE_DT ".<>
You may need to change the first condition to make sure that you select the correct subquery.
-edit
Not able to view a plan but you can invade the second join condition select and then the result of a subquery with a predicate according to your requirement. This should delay the or rating and leave only an equijoin (although rowsource return may be slightly larger than the opposite).
-Second edition, it did not work exactly when I tried it.
A hybrid of the previous two plans with a slight modification of how he was imitating the GOLD:
WITH h AS (SELECT / * + MATERIALIZE * / hi.message_id, hi.update_dt)
ROWNUM Clotilde
OF message_hist salvation)
SELECT i.MESSAGE_ID
i.UPDATE_DT
COALESCE(h.message_id,h2.message_id) message_id
, COALESCE (h.update_dt, h2.update_dt) update_dt
Clotilde COALESCE (h.rown, h2.rown)
Message FROM
LEFT JOIN: ON (i.message_id = h.message_id
AND i.update_dt = h.update_dt)
LEFT JOIN: h2 WE (DECODE(h.message_id,,i.message_id) = h2.message_id - only try this if previous join returned NULL
AND h2.update_dt<>
/
--------------------------------------------------------------------------------------------------------
| ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1. 66. 8 (0) | 00:00:01 |
| 1. TRANSFORMATION OF THE TEMPORARY TABLE. | | | | |
| 2. LOAD SELECT ACE | SYS_TEMP_0FD9D6605_28F27F | | | | |
| 3. COUNT | | | | | |
| 4. TABLE ACCESS FULL | MESSAGE_HIST | 150. 3300 | 2 (0) | 00:00:01 |
| 5. GLOBAL TRI | | 1. 66. | |
|* 6 | EXTERNAL RIGHT HASH JOIN | | 10497. 676K | 6 (0). 00:00:01 |
|* 7 | VIEW | | 150. 3300 | 2 (0) | 00:00:01 |
| 8. TABLE ACCESS FULL | SYS_TEMP_0FD9D6605_28F27F | 150. 3300 | 2 (0) | 00:00:01 |
|* 9 | OUTER HASH JOIN | | 328. 14432 | 4 (0) | 00:00:01 |
| 10. TABLE ACCESS FULL | MESSAGE | 200 | 4400 | 2 (0) | 00:00:01 |
| 11. VIEW | | 150. 3300 | 2 (0) | 00:00:01 |
| 12. TABLE ACCESS FULL | SYS_TEMP_0FD9D6605_28F27F | 150. 3300 | 2 (0) | 00:00:01 |
--------------------------------------------------------------------------------------------------------Information of predicates (identified by the operation identity card):
---------------------------------------------------6 - access("H2".") MESSAGE_ID "(+) = DECODE (TO_CHAR ('H'". "))" MESSAGE_ID"), NULL, 'I '. (("' MESSAGE_ID '))
7 - filter("H2".") UPDATE_DT "(+)<>
9 - access("I".") "UPDATE_DT" ="H" UPDATE_DT "(+) AND 'I'." "" ' MESSAGE_ID '="H" MESSAGE_ID "(+))"(This plan is another system if costs are not comparable)
-
Outer join - problem with the name of the table in the select list
Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production
create table (j1)
number of C1,
number of C2);
create table (j2)
number of C1,
number of C2);
insert into values j1 (1, 10);
insert into j1 values (1, 100);
insert into values j1 (1, 1000);
insert into values j2 (1, 2);
insert into values j2 (1, 4);
insert into values j2 (1, 8);
commit;
Select c1, j1.c2, j2.c2 of outer join of j1 j2 using (c1); - DOES NOT
Select c1, j1 j2 (c1) using outer join j2.c2. - WORK
Why?
Hello
Interesting question! Oracle goes very far in trying not to trigger an error.
The OUTER keyword (if used; it is always optional) must be preceded by one of the keywords, right or LEFT. Since neither LEFT, RIGHT, or FULL comes before OUTER in queries in your first message, it does not recognize as keyword OUTER and treats him like a table alias for table j1. Since j1 has an alias, the real table name cannot be used in the SELECT clause. This query is executed:
SELECT c1
outer.c2
j2.c2
External J1
Join the HELP of j2 (c1)
;
There is an INTERNAL join. Add to your sample data:
insert into values of j1 (-1, -10);
insert into values of j2 (-2, -20);
to be specified.
Moreover, USING is short for confUSING. I suggest you use IT for all the join conditions. Everyone (unless they you write or read a manual) does.
-
Outer join problem? test code included
Hi all
I have three tables:
Master: It's the program_codes, titles and the active indicator.
Main: Is the 'information sections"on a specific program.
ModeOfS: He intones the sections of main program (abstract e.t.c) full-time part-time (PT) or (FT).
They have all of the effective dates so when he ran during a given year knows whose registration was effective on this year here.
When I run for 2003 I get 4 records which is correct, but when I run for 2002, I have only 2 folders. There is no valid records in the 'ModeOfS' table, but there are three records in the table of the 'hand' that are valid. But missing registration of P1 of the table of the 'hand '.
I have external-joined the main table so I don't see why it doesn't show up. Any help would be great.
Basically, program information may be considered at the level of "ModeOfS" but should not be, it's that his different text. So if it does and it is in the selected year then I want to see it. The limits of two YEARS will always be the same thing that this query will run for one YEARHello
oraCraft wrote:
Hi allI have three tables:
Master: It's the program_codes, titles and the active indicator.
Main: Is the 'information sections"on a specific program.
ModeOfS: He intones the sections of main program (abstract e.t.c) full-time part-time (PT) or (FT).They have all application dates
I'm confused. In the sample data you posted, it seems that only 2 tables have dates of application. Did you post erroneous data, at least for the master by mistake?
so when he ran during a given year knows whose registration was effective on this year here.
When I run for 2003 I get 4 records which is correct, but when I run for 2002, I have only 2 folders. There is no valid records in the 'ModeOfS' table, but there are three records in the table of the 'hand' that are valid. But missing registration of P1 of the table of the 'hand '.What are the correct results for 2002? They are:
PROG TITLE A ID PROG TERM_EFF SECTION TEXT FK MO TERM_EFF SECTION TEXT ---- ------------- - -- ---- ---------- ------------ ----------------------- -- -- ---------- -------- ------------------- 0862 Mechanics Y P1 0862 2000 ABSTRACT This is Data 0862 Mechanics Y P2 0862 2000 OBJECTIVE This is Data 0862 Mechanics Y P3 0862 2000 PREREQUISITE This is Data
I have external-joined the main table so I don't see why it doesn't show up. ...
One of the conditions involving modeofs is an outer join:
... and main.ID = modeOfS.FK_ID(+)
but the other is not:
... and nvl(modeofs.TERM_EFF, 9999) = ( select nvl(max(m2.TERM_EFF), 9999) from modeofs m2 where m2.TERM_EFF <= 2003 and m2.SECTION = modeofs.SECTION and m2.MODE_OF_STUDY = modeofs.MODE_OF_STUDY)
When you use the old notation to join, if no table of x is always marked with a + sign (as in condition 1 above), then all the conditions involving the x table must have a + sign, otherwise the effect will be an inner join. If you find this confusing, you're not alone. It is one of the reasons why I prefer the ANSI join rating:
with ranked_main as ( select main.* , rank () over ( partition by SECTION order by TERM_EFF desc nulls last ) as MAIN_RANK from main where TERM_EFF <= :target_year or TERM_EFF is null ) , ranked_modeofs AS ( SELECT modeofs.* , rank () over ( partition by SECTION , MODE_OF_STUDY order by TERM_EFF desc nulls last ) as MODEOFS_RANK FROM modeofs WHERE TERM_EFF <= :target_year OR TERM_EFF is null ) select master.* , ranked_main.* , ranked_modeOfS.* from master join ranked_main on ranked_main.PROGRAM_CODE = master.PROGRAM_CODE and ranked_main.MAIN_RANK = 1 left outer join ranked_modeOfS on ranked_modeofs.FK_ID = ranked_main.ID and ranked_modeofs.MODEOFS_RANK = 1 ;
-
Hi all
I have issues with the help of ANSI LEFT JOIN compared to Oracle (+). Below test I did the same thing.
Here Query1 return all records of 4 same records regardless of emp_name = 'A' where as Query2 returns 2 records of ep_name = 'A' which is correct.create table emp (emp_id number(10), emp_name varchar2(50)); create table courses (course_id number(10), emp_id number(10), course_name varchar2(50)); INSERT INTO EMP values(1,'A'); INSERT INTO EMP values(2,'B'); INSERT INTO EMP values(3,'C'); INSERT INTO COURSES values(1,1,'ORACLE'); INSERT INTO COURSES values(2,1,'JAVA'); INSERT INTO COURSES values(3,3,'C#'); --*Query 1 SELECT a.*, b.* FROM EMP a LEFT JOIN COURSES b ON a.emp_id = b.emp_id AND a.emp_name = 'A' --*Query 2 SELECT a.*, b.* FROM EMP a, COURSES b where a.emp_id = b.emp_id(+) and a.emp_name = 'A'
Is this correct? I'm confused if you use the standard ANSI OUTER JOINS or not.
I am using Oracle 11g
Thank you.Change your AND WHERE, in the style of ANSI, select:
SELECT a.*, b.* FROM EMP a LEFT JOIN COURSES b ON a.emp_id = b.emp_id WHERE a.emp_name = 'A'
Filters in the clause are those who use the (+) in the Oracle syntax.
Filters WITHOUT (+) in the Oracle syntax should be in the WHERE clause using the ANSI syntax.Published by: Kim Berg Hansen on September 23, 2011 08:03
-
Left outer join on a derived Table
Hello:
I get an "ORA-00905: lack of keyword" error when I run the following query - don't know what I'm doing wrong here - any suggesstions will be very useful
Thank you
SELECT count (*)
FROM aradmin.ast_asset a left outer join
(
Select audit_hist_asset, Deploy_Time
of (a.asset_entry_id select audit_hist_asset, to_localtime (a.change_date, 'IS') Deploy_Time,)
ROW_NUMBER() over (partition by order of a.change_date a.asset_entry_id) rn
of aradmin.ast_bms_audit_history one
where a.change_type = 'status '.
and a.change_to = 'Deployed')
where rn = 1
) AS b
On a.asset_entry_id = b.audit_hist_assetHello
Good job on putting in shape! It is easier to read now.
The error message included a line number? You use a.asset_entry_id in two different places, to refer to two different tables. Who is confused, but niot necessarily a mistake. You should use different tables for aradmin.ast_asset aliases in the main query and aradmin.ast_bms_audit_history in the internal subquery.
Two of these tables have a column called asset_entry_id?
-
Dear elders,
Firstly, sorry if my question is considered to be 'very novice' but I have this problem of confusion.
Can I use the left outer join with summary?
For example:
Work table
Table No.JobCode JobGroupCode 111A 1100 112B 1100 113C 1100 121A 1200 333F 3300
Activity tableJobGroupCode ParentCode 1100 1000 1200 1000 1300 1000 3300 3000
I want to choose with this resultJobcode Mandays 111A 5 112B 7 113C 3
All I did was:Job.JobCode Job.JobGroupCode JobGroup.Parentcode Mandays 111A 1100 1000 5 112B 1100 1000 7 113C 1100 1000 3 121A 1200 1000 0 333F 3300 3000 0
and I got was only jobcode activity table, not exactly what I want to get all the work table jobcode. Could you please tell me, where I was wrong?select j.jobcode, j.jobgroupcode, jg.parentcode, sum(a.mandays) from job j inner join jobgroup jg on j.jobgroupcode = jg.jobgroupcode left join activity a on j.jobcode = a.jobcode group by j.jobcode, j.jobgroupcode, jg.parentcode
result
Thank you very much.Job.JobCode Job.JobGroupCode JobGroup.Parentcode Mandays 111A 1100 1000 5 112B 1100 1000 7 113C 1100 1000 3
Published by: user11197113 on May 25, 2009 03:31
Published by: user11197113 on May 25, 2009 03:32Hello (and welcome!).
Edit
OK, try this:
select j.jobcode, j.jobgroupcode, jg.parentcode, NVL(sum(a.mandays),0) from job j inner join jobgroup jg on j.jobgroupcode = jg.jobgroupcode left join activity a on j.jobcode = a.jobcode group by j.jobcode, j.jobgroupcode, jg.parentcode
That will give you this:
JOBC JOBG PARE SUM(A.MANDAYS) ---- ---- ---- -------------- 111A 1100 1000 5 112B 1100 1000 7 113C 1100 1000 3 121A 1200 1000 0 333F 3300 3000 0
These are real results of your test data.
-
SSRS for lack of outer join with the Oracle data source
It seems to be a problem with the Oracle driver used in the Reporting SERVICES query designer.
When you use an Oracle data source, if I create an outer join in the graphic designer, it automatically inserts '{OJ' before the join and '} ' after her. This is an incorrect syntax for Oracle and refuses to start. The curly braces and the JO editable in designer text, but if I go back to the graphic designer and immediately to reintegrate them.
Only, this has started to happen a year or two ago - before that it worked, but with the old (+) syntax.
Can it not be healed? It makes things very difficult.
-Geoff
Hi Geoff,
Thanks for posting in the Microsoft Community.
However, the question you posted would be better suited in the Forums of the Oracle Support; We recommend that you post your query in Oracle Support Forums to get help:
If you have any other questions or you need Windows guru, do not hesitate to post your questions and we will be happy to help you.
-
HELP SQL (auto / full outer join with date corresponding)
I'm having a hard time get this query nailed... hoping someone can help me sorted.
create table tab1 (identification number,
date of eff_date,
Code1 varchar2 (2),
Code2 varchar2 (2)
)
/
insert into tab1 values (2, to_date('2015-01-14','YYYY-MM-DD'), 'DAT', 'AS');
insert into tab1 values (2, to_date('2015-03-19','YYYY-MM-DD'), 'DAT', 'AS');
insert into tab1 values (2, to_date('2015-08-28','YYYY-MM-DD'), 'DAT', 'AS');
insert into tab1 values (2, to_date('2015-11-12','YYYY-MM-DD'), 'DAT', 'AS');
insert into tab1 values (2, to_date('2015-01-03','YYYY-MM-DD'), "DAT", "AE");
insert into tab1 values (2, to_date('2015-03-14','YYYY-MM-DD'), "DAT", "AE");
insert into tab1 values (2, to_date('2015-04-18','YYYY-MM-DD'), "DAT", "AE");
insert into tab1 values (2, to_date('2015-09-14','YYYY-MM-DD'), "DAT", "AE");
insert into tab1 values (2, to_date('2015-01-14','YYYY-MM-DD'), "DAT", "BS");
insert into tab1 values (2, to_date('2015-02-14','YYYY-MM-DD'), "DAT", "BS");
insert into tab1 values (2, to_date('2015-03-14','YYYY-MM-DD'), "DAT", "BS");
insert into tab1 values (2, to_date('2015-05-14','YYYY-MM-DD'), 'DAT', 'BE');
insert into tab1 values (3, to_date('2015-09-16','YYYY-MM-DD'), 'DAT', 'AS');
insert into tab1 values (3, to_date('2015-04-16','YYYY-MM-DD'), "DAT", "AE");
tab1
ID, date, code 1, code2
2. DID DAT 2015-01-14
2. DID DAT 2015-03-19
2. DID DAT 2015-08-28
2. DID DAT 2015-11-12
2 AE DAT 2015-01-03
2 AE DAT 2015-03-14
2 AE DAT 2015-04-18
2 AE DAT 2015-09-14
2 BS DAT 2015-01-14
2 BS DAT 2015-02-14
2 BS DAT 2015-03-14
BE DAT 2 2015-05-14
3. DID DAT 2015-09-16
3 AE DAT 2015-04-16
What I need to do...
1 auto join to match EI for each partition ID
2. THAT the date must be less than or equal to the date of the AE and when there is more then a line corresponding to this criterion has chosen the date of closest EI of the date of the ACE.
3. it must be a full outer join because I want to show all lines, even if it is not a match. There is a beginning, but not record end end gold but no record of departure
4. If there is an AE line for many AS lines (the SA date is less then equals the date of EI) then join this AE line to all 3 rows of ACE
5. the same rules for BS and BE.
result should look like this.
ID, date, code 1, id_1 code2, date_1, code1_1, code2_1
2 2015-01-14 DAT AS 2 AE DAT 2015-03-14
2 2015-03-19 DAT AS 2 AE DAT 2015-04-18
2 2015-08-28 DAT AS 2 AE DAT 2015-09-14
2 2015-11-12 DAT DID ZERO ZERO ZERO ZERO
NO NO NO NO 2 AE DAT 2015-01-03
2015-01-2 14 DAT BS 2 BE DAT 2015-05-14
2015-02-2 14 DAT BS 2 BE DAT 2015-05-14
2015 03-2 14 DAT BS 2 BE DAT 2015-05-14
3 2015-09-16 DAT DID ZERO ZERO ZERO ZERO
NO NO NO NO 3 AE DAT 2015-04-16
My attempt was somewhat along these lines (dealing only with SA / combos AE) but it does not manage the many scenarios one (req 4).
Select a.*, b.* from
(select row_number () on the rn (partition by a.id order a.eff_date), a.*)
of tab1 where a.code2 = 'AS') a
full outer join
(select row_number () on the rn (b.eff_date order by b.id partition), b.*)
tab1 b where b.code2 = 'Æ') b
on a.id = b.id
and a.rn = b.rn
and a.eff_date < = b.eff_date
Hello
owbdev99 wrote:
I'm having a hard time get this query nailed... hoping someone can help me sorted.
create table tab1 (identification number,
date of eff_date,
Code1 varchar2 (2),
Code2 varchar2 (2)
)
/
insert into tab1 values (2, to_date('2015-01-14','YYYY-MM-DD'), 'DAT', 'AS');
...
Thanks for posting the CREATE TABLE and INSERT. I know it can be a lot of trouble. You want to get answers that work, not you? Make sure that the statements you post too much work. Test (and, if necessary, attach) your statements before committing. You said code1 be VARCHAR2, but all the instructions insertion have values of 3 characters for code1.
You are on the right track, with an analytical function, but ROW_NUMBER solves this problem. 1 "THAT line" could correspond to the 1st, 2nd, 3rd or any other line 'AE' and vice versa. Try to use the analytical MIN function instead or ROW_NUMBER, like this:
WITH got_next_e_date AS
(
SELECT id, eff_date, code1, code2
MIN (CASE
WHEN SUBSTR (code2, 2) = 'E '.
THEN eff_date
END
) OVER (PARTITION BY ID.
, SUBSTR (code2, 1, 1)
ORDER BY eff_date DESC
) AS next_e_date
OF tab1
)
s AS
(
SELECT *.
OF got_next_e_date
"WHERE SUBSTR (code2, 2) s ="
)
e
(
SELECT *.
OF got_next_e_date
WHERE SUBSTR (code2, 2) = 'E '.
)
SELECT s.id
s.eff_date
s.code1
s.code2
e.id AS id_1
e.eff_date AS eff_date_1
e.code1 AS code1_1
e.code2 AS code2_1
S
FULL OUTER JOIN e ON s.id = e.id
AND s.next_e_date = e.eff_date
AND SUBSTR (s.code2, 1, 1) = SUBSTR (e.code2, 1, 1)
ORDER OF NVL (s.id, e.id)
, NVL (SUBSTR (s.code2, 1, 1)
, SUBSTR (e.code2, 1, 1)
)
s.eff_date
;
Out (as you asked):
ID EFF_DATE CODE1, CODE2 ID_1 EFF_DATE_1 CODE1_1 CODE2_1
--- ---------- ----- ----- ----- ---------- ------- -------
2 2015-01-14 DAT AS 2 AE DAT 2015-03-14
2 2015-03-19 DAT AS 2 AE DAT 2015-04-18
2 2015-08-28 DAT AS 2 AE DAT 2015-09-14
2. DID DAT 2015-11-12
2 AE DAT 2015-01-03
2015-01-2 14 DAT BS 2 BE DAT 2015-05-14
2015-02-2 14 DAT BS 2 BE DAT 2015-05-14
2015 03-2 14 DAT BS 2 BE DAT 2015-05-14
3. DID DAT 2015-09-16
3 AE DAT 2015-04-16
I guess code2 is always 2 characters, and the 2nd character is always ' or 'E '.
I assume that the combination [id, eff_date, code2] is unique.
If these assumptions are wrong, you need a few minor changes, but nothing big.
-
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.
-
Oracle: Use LEFT OUTER JOIN, but convert the data to an external list
Hello, all,.
I know it can be done; I just don't remember how I got it done, oh there are so many years.
Assumes that the tables exist for groups and individuals. People can belong to several groups.
SELECT g.groupName, p.lastName || ', ' || p.firstName as fullName FROM groups g LEFT OUTER JOIN groupPersonAssociation gpa ON gpa.groupID = g.groupID LEFT OUTER JOIN person p ON p.personID = gpa.personID ORDER BY g.groupName, fullName
This gives us:
Group One Alpha, Daniel Group One Bravo, Charles Group One Charlie, Chuck Group Two Beta, Alpha Group Two Delta, Bonnie Group Three Echo, Bunny Group Three Golf, Samuel Group Three November, Stan
How word the SQL to get the data as:
Group One Alpha, Daniel | Bravo, Charles | Charlie, Chuck Group Two Beta, Alpha | Delta, Bonnie Group Three Echo, Bunny | Golf, Samuel | November, Stan
V/r,
^_^
I finally thought to it. I was using incorrect keywords on Google.
SELECT g.groupName, LISTAGG(p.lastName || ', ' || p.firstName,' | ') WITHIN GROUP (ORDER BY g.groupName) "fullName" FROM groups g LEFT OUTER JOIN groupPersonAssociation gpa ON ggpa.groupID = g.groupID LEFT OUTER JOIN person p ON p.personID = gpa.personID GROUP BY g.groupName ORDER BY g.groupName, fullName
Just in case someone else is going through this same desire.
HTH,
^_^
-
using outer joins if the two column is null? Use only (+)
Hi all
create the table xxc_tr_num (tl_number number, tr_no number tl_no_id);
insert into xxc_tr_num values (123,100,222);
insert into xxc_tr_num values (124,100,333);
create the table xxc_od_tab (tl_number number, tl_id number);
insert into xxc_od_tab values (123,001);
insert into xxc_od_tab values (null, null);
create table xxc_oth_tab (name varchar2 (10), number of tl_id);
insert into xxc_oth_tab values('abc',,001);
insert into xxc_oth_tab values (null, null);
Wait it out put
tr_no tl_no_id name
100 222 abc
100 333
using outer joins if the two column is null? use only please of outer joins
And I tried to use outer joins on both tl_id column but not get values and I use have County (tr_no ) > 1
Rajesh123 wrote:
Thank you Kiss it is not possible to use having clause?
You need to understand the functioning of the group. If you will not be asked this question.
Check this box
SQL> select tr_no, 2 tl_no_id, 3 count(*) 4 from xxc_tr_num a, 5 xxc_od_tab b, 6 xxc_oth_tab c 7 where a.tl_number = b.tl_number(+) 8 and b.tl_id = c.tl_id(+) 9 group 10 by tr_no 11 , tl_no_id; TR_NO TL_NO_ID COUNT(*) ---------- ---------- ---------- 100 333 1 100 222 1
See what returns the count? You have grouped according to TR_NO and TL_NO_ID. You must take into consideration the TL_NO_ID just put COUNT (TR_NO) does not increase the NUMBER of the whole group. To get the NUMBER on the whole group, I used the analytical function and did. Like this, see the number of the analytical function here
SQL> select tr_no, 2 tl_no_id, 3 count(*), 4 count(*) over(partition by tr_no) 5 from xxc_tr_num a, 6 xxc_od_tab b, 7 xxc_oth_tab c 8 where a.tl_number = b.tl_number(+) 9 and b.tl_id = c.tl_id(+) 10 group 11 by tr_no 12 , tl_no_id; TR_NO TL_NO_ID COUNT(*) COUNT(*)OVER(PARTITIONBYTR_NO) ---------- ---------- ---------- ------------------------------ 100 222 1 2 100 333 1 2
So to answer your question, yes you can't do in the HAVING clause...
-
Hi all
I use under version
Connected to Oracle Database 11g Express Edition Release 11.2.0.2.0
SQL > SELECT E.ENAME,.
2 D.DEPTNO,
3 D.LOC
4. TO EMP E,.
DEPT 5 D
6. WHERE = E.DEPTNO D.DEPTNO (+);
ENAME, DEPTNO LOC
---------- ------ -------------
DALLAS SMITH 20
ALLEN 30 CHICAGO
WARD 30 CHICAGO
20 DALLAS JONES
MARTIN 30 CHICAGO
BLAKE 30 CHICAGO
CLARK 10 NEW YORK
SCOTT 20 DALLAS
THE 10 NEW YORK KING
TURNER 30 CHICAGO
20 DALLAS ADAMS
JAMES 30 CHICAGO
FORD 20 DALLAS
MILLER 10 NEW YORK
40 BOSTON
15 selected lines
-----------------------------------------------------------------------------------------------------------------------------
SQL > SELECT E.ENAME,.
2 D.DEPTNO,
3 D.LOC
4. TO EMP E
5 LEFT OUTER JOIN
D 6 DEPT
7. THE E.DEPTNO = D.DEPTNO;
ENAME, DEPTNO LOC
---------- ------ -------------
MILLER 10 NEW YORK
THE 10 NEW YORK KING
CLARK 10 NEW YORK
FORD 20 DALLAS
20 DALLAS ADAMS
SCOTT 20 DALLAS
20 DALLAS JONES
DALLAS SMITH 20
JAMES 30 CHICAGO
TURNER 30 CHICAGO
BLAKE 30 CHICAGO
MARTIN 30 CHICAGO
WARD 30 CHICAGO
ALLEN 30 CHICAGO
14 selected lines
My doubt is both are same query is the same, is in ansi format and is in the format of the Oracle,.
but the results are different.
For the first query null is coming for unmatched records in the dept table
but in the second query, it does not come
Thank you
Hello
2947022 wrote:
Hi all
I use under version
Connected to Oracle Database 11g Express Edition Release 11.2.0.2.0
SQL > SELECT E.ENAME,.
2 D.DEPTNO,
3 D.LOC
4. TO EMP E,.
DEPT 5 D
6. WHERE = E.DEPTNO D.DEPTNO (+);
ENAME, DEPTNO LOC
---------- ------ -------------
DALLAS SMITH 20
ALLEN 30 CHICAGO
WARD 30 CHICAGO
20 DALLAS JONES
MARTIN 30 CHICAGO
BLAKE 30 CHICAGO
CLARK 10 NEW YORK
SCOTT 20 DALLAS
THE 10 NEW YORK KING
TURNER 30 CHICAGO
20 DALLAS ADAMS
JAMES 30 CHICAGO
FORD 20 DALLAS
MILLER 10 NEW YORK
40 BOSTON
15 selected lines
-----------------------------------------------------------------------------------------------------------------------------
SQL > SELECT E.ENAME,.
2 D.DEPTNO,
3 D.LOC
4. TO EMP E
5 LEFT OUTER JOIN
D 6 DEPT
7. THE E.DEPTNO = D.DEPTNO;
ENAME, DEPTNO LOC
---------- ------ -------------
MILLER 10 NEW YORK
THE 10 NEW YORK KING
CLARK 10 NEW YORK
FORD 20 DALLAS
20 DALLAS ADAMS
SCOTT 20 DALLAS
20 DALLAS JONES
DALLAS SMITH 20
JAMES 30 CHICAGO
TURNER 30 CHICAGO
BLAKE 30 CHICAGO
MARTIN 30 CHICAGO
WARD 30 CHICAGO
ALLEN 30 CHICAGO
14 selected lines
My doubt is both are same query is the same, is in ansi format and is in the format of the Oracle,.
but the results are different.
For the first query null is coming for unmatched records in the dept table
but in the second query, it does not come
Thank you
In fact, these requests are not the same.
The first is to find all the lines of the Department, with the corresponding lines of PGE (when there are). This is equivalent to «FROM dept LEFT OUTER JOIN emp...» ».
The second is to find all the rows in the emp of the lines of the Department (when there are any). This is equivalent to «...» WHERE e.deptno = d.deptno (+).
-
Hi, I have a problem with my query:
SELECT t1.step, t2.name FROM RIGHT JOIN t1 t2 ON t2.step = t1.step
I have two tables, small t1 and t2 great.
table T1 (7 lines):
name of the step
1 a
2B
3 C
4 D
5 e
$4
7 g
table T2 (10000 lines);
name of the step ID
100 1A
101 2B
102 3 c
103 4 D
104 5th
105 1A
106 2 b
107 3 c
108 d 4
109 5 e
110 1A
111 2B
112 3 c
113 4 D
114 5 e
115 1A
116 2B
117 c 3
118 d 4
119 5th
… … ..
I want external right to join them, and have values in t2.name NULL and the range of steps (1-7).
My query returns this:
name of the step
1 a
2B
3 C
4 D
5 e
1 a
2B
3 C
4 D
5 e
1 a
2B
3 C
4 D
5 e
....
Thank you.
So there is indeed a key score in T2, it is "ROW_NR", and we can do this:
select t1.col_nr , t2.row_nr , t1.sheet_name , t1.cell , t1.column_name , t2.string_val from t1 left outer join t2 partition by (t2.row_nr) on t2.col_nr = t1.col_nr ;
Maybe you are looking for
-
Cannot start more Apple Remote Desktop
Hi all All of a sudden I can't launch ARD 3.8 (A 380, 95) in El Capitan 11.11.4 more. Whenever I try to launch it on one of my Mac, I get a warning telling me that he was able to start "because of an unexpected error" (The Remote Desktop software is
-
Visual Land V-Touch Pro ME - 965L 4GB Media Player
I recently bought the MP3 player above. I'm trying to find the answer to download songs from a standard CD to the player via my personal desktop PC MP. Help
-
Problem of CPU ThinkPad Edge 14
Hey So I had my Thinkpad Edge 14 since the end of last summer. I keep putting it off, but now it has got to the point where I have enough of him. Here's my problem: I connect my Thinkpad Edge 14 in the charger, which is obviously plugged into an ou
-
Shift caps lock flashing but the computer usually turn on g6
Just my computer stopped working. I turn it on and it acts like it will start but then nothing. The Caps light is on but nothing else, not to mention that the power button. All of the suggestions. I took out the battery and you just plug it in and t
-
I am trying to access my Gmail account, but all of a sudden, it won't open.
Original title: Sudden Mail problem I am trying to access my Gmail account, but all of a sudden, it won't open. The search bar flashes back to this--> https://Accounts.google.com/ServiceLogin?service=mail&passive=true&RM=false&continue= this--> is h