Multiple table on Outer Join
Hi,
Does anyone knows how you can explain, in ANSI SQL 1999, the outer join with more than two tables ?
Here is an example in the old Oracle 8i syntax:
select a.col1,b.col2,c.col3
from a, b, c
where a.col1 = b.col1 (+) and
a.col1 = c.col1 (+)
Thank in advance
Alessandro
Try this:
select a.col1, b.col2, c.col3
from a left outer join b on (a.col1 = b.col1)
left outer join c on (a.col1 = c.col1);
Similar Messages
-
OJ syntax for multi-table left outer join with MS Oracle Driver
I have a multi-table left outer join that works fine in SQL Server ODBC Driver, Oracle ODBC driver 8.01.07.00, but not with Microsoft ODBC Driver for Oracle 2.573.7326.0
SELECT * from { oj A LEFT OUTER JOIN B ON A.col1 = B.col1 LEFT OUTER JOIN C ON A.col1 = C.col1 }
I noticed someone had a similar problem (the proposed solution doesn't work):
http://www.justpbinfo.com/listarchive/msg02874.html
Does anyone know how to get this working with the Microsoft ODBC Driver for Oracle? Or does it just not work?The Microsoft ODBC Driver for Oracle 2.573.7326.0 does perform the same 'fix up' with {oj} in Oracle 8i. The problem is that it doesn't work when joining more than two tables:
This works:
SELECT * from { oj A LEFT OUTER JOIN B ON A.col1 = B.col1}
This doesn't work:
SELECT * from { oj A LEFT OUTER JOIN B ON A.col1 = B.col1 LEFT OUTER JOIN C ON B.col1 = C.col1 }
(The second query will work with the Oracle Oracle ODBC driver, with a bit of tweaking. But I haven't found a way to get it to work with the Microsoft ODBC Driver for Oracle 2.573.7326.0. My suspicion is that it just doesn't work.)
Gavin -
How to use common object from two tables with out join.
HI,
I have two tables called A & B In A table i have the following objects
1.weekend
2.S1(measure)
3.S2(measure)
4.S3(measure)
5.S4(measure)
And In B table i have followning columns
1.week end
2.p1(measure)
3.p2(measure)
4.p3(measure)
5.p4(measure)
Now in universe i created all the measure objects i.e.s1,s2,s3,s4,p1,p2,p3,p4 A.weekend,B.weekend.
instead of using week end two times i wnt to use only once because this is common in both table.
if i use join between these tables i am getting values fine
But With out join is there any thing to do in universe level to create common objects to use from both the tables..I tried using aggregate awareness but while reporting it is taking as two SQL.which is not synchronized.
Please help me on this ...hi,
Although Weekend column is present in both tables, by creating a single Object in Universe, Universe can identify relationship with only table referenced in Object Creation.
So, there will be no identification of relationship with other table measures.
Obviously, you need to create 2 Weekend objects in Universe (in two classes).
Case 1: You need not join these two tables in Universe. When you create 2 Queries in WEBI, automatcially Weekend objects are synchronized (if both are of same datatype)
Case 2: If you join these two tables in Universe, Obviously,
your SQL may contain Weekend from Table1, measures from Table 2
or
your SQL may contain Weekend from Table2, measures from Table 1
Finally, You need to create 2 objects in Universe. But your query may contain a single Object based on Case 2.
Regards,
Vamsee -
Joining multiple tables using ANSI join
Hi,
I need to join 4 tables using ANSI join. Lets say they are A,B,C adn D. A and B has a common column called x. C and D had a common column called y. I wrote a query like this
select * from A left outer join B on (A.x=B.x),
C left outer join D on (C.y =D.y)
its not working!!!
I need to use ANSI join.
can any body help me please???Hi,
But is there is any common column between A,C or B,C or D,A?
Let me know. -
SAP Query - Need to join 3 Tables via outer join
Hi,
I need to join 3 Tables using SAP Query. I wish an OUTER JOIN to be performed for the table join.
For Example:
Table 1 has 1000 Entries Field A Field B
Table 2 has 300 Entries Field A Field C
Table 3 has 100 Entries Field A Field D
The normal Join (INNER JOIN) gives me only the records that exists in all the 3 Tables.
But what i need is:
In the above example, If one entry in Table 1 has no matching records in Table 2 / Table 3, there should be an output entry in the SAP Query like
Field A Field B Field C Field D
xxxx yyyy Blank Blank
If there is a common record that exists in the tables, that record should appear in the same entry in the Query output.
Field A Field B Field C Field D
xxxx yyyy zzzz aaaa
In this way, there should be a minimum of 1000 entries (Largest no of records in the Tables joined). More than 1000 records in the Query output depends on the number of common records.
Kindly help if you have come across such a scenario.
thanks & regds
sriramHi
Please join the outer join as below
Table1 with Field A to Table 2 Field A-----outer join
Table1 with Field A to Table 3 Field A------outer join
then you get the out put as per your requirement
Regards
Damu -
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!
QianThank 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 -
Left Outer Joining multiple tables to one source table FAILS with VLD-1511
Hi all,
Is it me, or is OWB unable to handle left outer joining 1 source table to multiple other tables?
I want to load a fact table so I have 1 source table with measures. This table must be outer joined to some dimensions that have their FK in the fact table.
The SQL statement would look like this (and is perfectly valid):
select ...
from input, dim1, dim2
where input.c1 = dim1.c1(+)
and input.c2 = dim2.c2(+);
I put the where clause in the joiner operator and validate, but that gives me message VLD-1511: A table may be outer joined to at most one other table.
Even splitting this up into one outer join per joiner still gives this message.
A search and look around on the forum and on metalink shows there are related issues (like bug 3334035). Seemingly creating a view is the work-around to use.....? (ie downgrading owb to a simple gui tool) }-;
Have other people experienced this problem of not being able to outer join one input table to multiple other tables?
Thanks,
EdI have had some feedback from Oracle. It turns out this has to do with 2 issues. Below I have pasted the text that Support gave me:
<---------- START QUOTE ---------->
RESEARCH
=========
Bug 3437036 KEY LOOKUP DOES NOT DETECT ORA-1417 IN VALIDATE/GENERATE STEP
Unpublished Bug 4211684 FORWARD PORT OF BUG 3437036
shows:
Some more development has been completed when this bug is fixed in Paris.
The following are the details:
1. If the join condition contains a full outer join such as
tab1.c (+) = tab2.c (+) and tab2.c (+) = tab3.c
then the new validations implemented for this bug do not apply since
in OWB, full outer join triggers generation of joins in ANSI syntax.
ANSI syntax does not have the original problem the base bug of this
bug reported.
2. If the join condition does not contain any full outer join condition,
then the join is generated in Oracle join syntax, which is subject two
several restrictions. The fix to this bug check two of the restrictions.
3. The first restriction in Oracle syntax is that the outer join operator
"(+)" can only directly be attached to a column name. If you attach it
to an expression, such as the following:
(tab1.c + 1) (+) = tab2.c
Then there will be an ORA-936 error at the time of mapping deployment.
For this case, I have added a validation message VLD-1512 to error out
this situation.
4. The second restriction in Oracle syntax is that a table can only be
outer joined to exactly one other table.
For example, this is an invalid join in Oracle syntax:
tab1.c (+) = tab2.c and tab1.d (+) = tab3.d
because tab1 is left outer joined to tab2 and tab3.
But note that the following is still valid in Oracle syntax:
tab1.c (+) = tab2.c and tab1.d = tab3.d (+)
because tab1 is left outer joined to tab2 and right outer joined to tab3.
So this latter case does not violate the restriction that "same oj" to
more than 1 table is not allowed.
If same oj to more than 1 table is specified in a join condition,
VLD-1511 will be issued, and the map is made invalid.
<---------- END QUOTE ---------->
OWB does a partial validation, ie not all access paths are (can be) checked. A full check is only done by the database itself. So some scenarios (like checking whether multiple tables are outer joined the correct way) are not checked, and in this case are flagged with an error (even though it is actually a correct scenario).
Seemingly this was not flagged with an error in earlier versions of OWB, so beware, OWB behaviour may change when upgrading...
Alternative solutions are (1) using key lookups, (2) using a view with all outer joins in there, (3) using intermediate result tables between the joins.
Hope this info helps some people prevent spending too much time on a false error message,
Ed -
Outer join between logical tables
Hello,
This question is likely to be asked many times, but I failed to find the proper thread in the forum.
Assume there are 2 logical tables "Fact" and "Dim".
"Fact" has 1 LTS which consists of physical tables F, FX1, FX2 which are inner joined.
"Dim" has 1 LTS which consists of physical tables D, DX1, DX2 which are inner joined.
F and D tables are also joined together on physical layer.
I define left outer join between "Fact" and "Dim" on logical layer.
I create a request in Answers, querying columns from "Fact" and "Dim" which map to physical tables F and D only.
I expect OBIEE to build SQL query which uses F and D tables only, outer joined.
Instead of that all the physical tables used for logical tables "Fact" and "Dim" are joined together in a form like:
SELECT F.col, D.col
FROM (F inner join FX1 inner join FX2) left outer join (D inner join DX1 inner join DX2)
Is there any way to avoid this behavior or build data model in different way?
Thank you!Hi Alex,
In BI Applications you never have a null in a facts foreign key to the dimension. Instead there is always a zero row wi record inserted with 'Unspecified' in all the columns. When the fact table is populated in the ETL, if the fact record doesn't have a corresponding dimension record the WID is poulated with zero.
This removes the problems with outer joins and helps considerably with performance.
This could be one practice you take from BI Apps and put into your own ETL's and data model.
Regards
Robin -
Merge or Union Multiple Tables to one big table
Hi All,
I am new to SQL and learning great. I have a use to merge multiple tables. Every table has different kind of data but BP ID is same in every table (but some table have more BP IDs and some are less). The below is sample…
Table A
BPID - Cost – Profit
0001 – 10 -4
0002 -11 - 3
Table B
BPID - Name – Address
0003 – John - Dallas
0002 –Mark - Huston
Table C
BPID – Zip code – State
0004- 74001 – TX
0002-75008 – TX
Output (after merging in Table A+B+C) would be like this…
BPID – Cost – Profit – Name – Address- Zip Code – State
0001 -10- 4 - NULL -- NULL – NULL – NULL
0002 – 11 – 3 - Mark - Huston ---75008 – TX
0003 – NULL – NULL – John – Dallas – NULL – NULL
0004 – NULL – NULL – NULL – NULL – 75008 – TX
Thanks for your help in this.I am including you a full example:
create table TableA (BPID VARCHAR(4), Cost INT, Profit INT)
create table TableB (BPID VARCHAR(4), Name VARCHAR(MAX), Address VARCHAR(MAX))
create table TableC (BPID VARCHAR(4), ZipCode VARCHAR(5), State VARCHAR(2))
INSERT INTO TableA VALUES('0001',10,4),('0002',11,3)
INSERT INTO TableB VALUES('0003','John','Dallas'),('0002','Mark','Huston')
INSERT INTO TableC VALUES('0004','74001','TX'),('0002','75008','TX')
SELECT ISNULL(ISNULL(TableA.BPID, TableB.BPID), TableC.BPID) AS BPID,
TableA.Cost, TableA.Profit, TableB.Name, TableB.Address, TableC.ZipCode, TableC.State FROM TableA
FULL OUTER JOIN TableB ON (TableA.BPID = TableB.BPID)
FULL OUTER JOIN TableC ON (TableB.BPID = TableC.BPID)
And the results: -
SAP QUERY/INFOSET with OUTER JOIN
Hi,
I have created an infoset (SQ02) using two tables for SAP query.
Table: AGR_TEXTS. Fields: AGR_NAME, SPRAS, LINE, TEXT.
TABLE: AGR_FLAGS. Fields: AGR_NAME, FLAG_TYPE, FLAG_VALUE.
Joined these two tables using outer join. Defined the join condition as AGR_NAME = AGR_NAME. Saved and Generated the infoset.
Created SAP Query (SQ01) using the above created infoset.
In the basic list,
Marked AGR_NAME (AGR_TEXTS), TEXT, FLAG_VALUE as listed fields.
Marked AGR_NAME (AGR_TEXTS), SPRAS, LINE, FLAG_TYPE as selection fields.
Save the query. When execute the query provided the inputs for all the selection fields in the selection screen as given below:
AGR_NAME=Z*
SPRAS=E
LINE=00000
FLAG_TYPE=LICENSE_01
Executed the Query.
Got the results only the matched records as per the selection field FLAG_TYPE.
But I want all the records from table AGR_TEXTS and matched records from table AGR_FLAGS.
I want the report (output) as below,
_AGR_NAME TEXT FLAG_VALUE_
ZCA_BASIC_ACCESS_GLOBAL Non-critical basic access for all users. 53
ZCA_BASIC_ENDUSER_ACCESS UK:CA Basic Enduser Access
ZCA_BASIC_ENDUSER_ACCESS_UK UK:CA Basic Enduser Access UK 52
ZCA_BASIC_ENDUSER_SU52_UK UK:CA Basic Enduser Access to change Parameter ID's
But I got the report (output) as below,
_AGR_NAME TEXT FLAG_VALUE_
ZCA_BASIC_ACCESS_GLOBAL Non-critical basic access for all users. 53
ZCA_BASIC_ENDUSER_ACCESS_UK UK:CA Basic Enduser Access UK 52
Apprecited your help. Thanks.
Code Formatted by: Alvaro Tejada Galindo on Dec 30, 2009 2:20 PMIdentify a KF for which there are non-zero values for all accounts in the cube.
Create the query with Account (from MP coming from both IO and cube) and this KF. KF will have 0 values for accounts not existing in the cube. Create a condition to show only the 0 value for the KF. This should show you all the accounts in the IO which are not in the cube. -
How to use outer join to display the results
I want to develop a report in which the column sequence will be as follows:
1)Item_code
2)Item_name
3)Stock
4)Pending
5)Re-order Level
6)Item_reorder_qty
I had written the query, but the problem is all the products does not get displayed. The products are more than 700. But after running the query, only 364 gets displayed.
How i can make use of outer join in the following statements :
AND ITEM_CODE = LCS_ITEM_CODE
AND ITEM_CODE = PI_ITEM_CODE
AND LCS_ITEM_CODE = PI_ITEM_CODE
in the below given query to solve this.
SELECT
ITEM_CODE,
ITEM_NAME,
((LCS_STK_QTY_BU + LCS_RCVD_QTY_BU - LCS_ISSD_QTY_BU- LCS_REJECT_QTY_BU)/IU_MAX_LOOSE_1)STK ,
SUM(TO_NUMBER(PI_QTY||'.'||PI_QTY_LS) - (PI_GI_QTY_BU/IU_CONV_FACTOR/IU_MAX_LOOSE_1))PNDG,
ITEM_RORD_LVL,
ITEM_RORD_QTY
FROM
OM_ITEM,
OM_ITEM_UOM,
OT_PO_HEAD,
OT_PO_ITEM,
OS_LOCN_CURR_STK
WHERE (ITEM_UOM_CODE = IU_UOM_CODE AND ITEM_CODE = IU_ITEM_CODE)
AND ITEM_CODE = LCS_ITEM_CODE
AND ITEM_CODE = PI_ITEM_CODE
AND LCS_ITEM_CODE = PI_ITEM_CODE
AND LCS_LOCN_CODE ='RM'
AND PI_PH_SYS_ID = PH_SYS_ID
GROUP BY ITEM_CODE,ITEM_NAME,LCS_STK_QTY_BU,LCS_RCVD_QTY_BU,LCS_ISSD_QTY_BU,LCS_REJECT_QTY_BU,IU_MAX_LOOSE_1,ITEM_RORD_LVL,ITEM_RORD_QTY
ORDER BY ITEM_CODE ASC
YogeshAs you have no table aliases I can't tell what columns relate to which tables on your join conditions.
Can you not just put (+) next to the outer joined columns you require on the where clause? Or perhaps consider using ANSI syntax for the joins such as <tableA> LEFT OUTER JOIN <tableB> ON (<join conditions>) -
제품 : ORACLE SERVER
작성날짜 : 2002-04-10
OUTER JOIN 에 대하여
====================
Purpose
Outer join의 효과과 이용방법에 대해 이해한다.
Explanation
1. 개념
다음의 용어에 대해 우선 살펴보자 :
1) outer-join column - symbol(+) 을 사용하는 column 이다 .
예를 들어 EMPNO(+) ,DEPT.DEPTNO(+) 는 outer join column들이다.
2) simple predicate - AND , OR,NOT 을 가지지 않는 단순한 관계표현으로
A=B 의 관계로 표현된다.
3) outer join predicate - 한개 이상의 outer join column 을 갖는 simple
predicate 이다.
2. OUTER JOIN 사용법 - RULES
outer join predicate 는 오직 1 table 의 column 들 만이 outer join
column 으로 이용되어져야 한다. 즉 한 single outer join predicate 의
모든 outer join column 은 모두 같은 table이어야 한다.
이런 취지에서 다음 statement 는 틀린 것이다.
EMP.EMPNO(+) = DEPT.DEPTNO(+)
이것은 두 table 의 outer join column 들이다.
한 predicate 의 한 column 이 outer join column 이면 같은 table 의 모든
column 은 outer join column 이어야 한다.
이 취지에서 다음 문장은 틀린 것이다.
EMP.SAL + EMP.COMM(+) = SALGRADE.HIGH
한 table 의 column 들이 outer join 것과 아닌 것과 outer join 인것으로
섞여있기 때문이다.
predicate 에서 (+) 표시가 붙은 table 은 다른 table 을 direct 하게
outer join 한다. indirect 하게 다른 tabe 을 outer join 한다는 것은
그들 table 자체가 또 outer join 하는 경우이다.
이 경우 한 table 은 direct하게든 indeirect 하게든 자기 자신에게 outer
join 하는 경우는 허용되지 않는다.
다음의 문장은 이런 취지에서 틀린 경우이다.
EMP.EMPNO(+) = PERS.EMPNO
AND PERS.DEPTNO(+) = DEPT.DEPTNO
AND DEPT.JOB(+) = EMP.JOB - circular outer
join relationship
3. OUTER JOIN 실행
주어진 table T 에는 outer join 과 non-outer join 이 있다.
실행시 다음처럼 수행된다.
1) The result of joining all tables mentioned in table T's
outer join predicates is formed ( by recursive application
of this algorithm ).
2) For each row of the result, a set of composite rows is
formed, each consisting of the original row in the
result joined to a row in table T for which the composite
row satisfies all of table T's outer join predicates.
3) If a set of composite rows is the null set, a composite
row is created consisting of the original row in the
result joined to a row similar to those in table T, but
with all values set to null.
4) Rows that do not pass the non-outer join predicates are removed.
This may be summarised as follows. Outer join
predicates ( those with (+) after a column of table T ), are
evaluated BEFORE table T is augmented with a null row. The null
row is added only if there are NO rows in table T that satisfy
the outer join predicates. Non-outer join predicates are
evaluated AFTER table T is augmented with a null row (if needed)
4. OUTER JOIN - RECOMMENDATIONS
Certain types of outer joins in complicated logical
expressions may not be well formed. In general, outer join
columns in predicates that are branches of an OR should be
avoided. Inconsistancies between the branches of the OR can
result in an ambiguous query, and this may not be detected. It
is best to confine outer join columns to the top level of the
'where' clause, or to nested AND's only.
5. OUTER JOIN - ILLUSTRATIVE EXAMPLES
1) Simple Outer Join
SELECT ENAME, LOC
FROM DEPT, EMP
WHERE DEPT.DEPTNO = EMP.DEPTNO(+)
The predicate is evaluated BEFORE null augmentation. If
there is a DEPT row for which there are no EMP rows, then a null
EMP row is concatenated to the DEPT row.
2) Outer Join With Simple Post-Join Predicates
SELECT ENAME, LOC
FROM DEPT, EMP
WHERE DEPT.DEPTNO = EMP.DEPTNO(+)
AND EMP.DEPTNO IS NULL
The second simple predicate is avaluated AFTER null
augmentation, since there is no (+), removing rows which were
not the result of null augmentation and hence leaving only DEPT
rows for which there was no corresponding EMP row.
3) Outer Join With Additional Pre-Join Predicates
SELECT ENAME, LOC
FROM DEPT, EMP
WHERE DEPT.DEPTNO = EMP.DEPTNO(+)
AND 'CLERK' = EMP.JOB(+)
AND EMP.DEPTNO IS NULL
The predicate on EMP.JOB is evaluated at the same time
as the one on EMP.DEPTNO - before null augmentation. As a
result, a null row is augmented to any DEPT row for which there
are no corresponding clerks's in the EMP table. Therefore, this
query displays departments containing no clerks.
Note that it the (+) were omitted from the EMP.JOB
predicate, no rows would be returned. In this case, both the
EMP.JOB and EMP.DEPTNO IS NULL predicates are evaluated AFETR
the outer join, and there can be no rows for which both are
true.I had to put it in a subquery? (if that's what it's called)
SELECT a1.date_field DateAndHour, b1.OR_date, NVL(b1.record_count,0)
FROM MASTER_DATE_TABLE a1,
(SELECT TO_CHAR(b.OR_IN_DTTM,'YYYYMMDDHH24') OR_date, COUNT(*) record_count
FROM hsa_tgt.PICIS_OR b
GROUP BY TO_CHAR(b.OR_IN_DTTM,'YYYYMMDDHH24')) b1
WHERE a1.date_field = b1.OR_date (+)
GROUP BY a1.date_field, b1.OR_date, b1.record_count
HAVING (TO_DATE(a1.date_field,'YYYYMMDDHH24') BETWEEN '01-Jan-2006' AND '31-Jan-2006')
ORDER BY a1.date_field; -
Outer join with effective date in peoplesoft query
Hi,
I'm trying to join two tables using outer join. Both tables are effective dated:
Dept_Tbl: dept_id, status, effdt
Tips_Tbl: dept_id, tips_id, effdt
Not all records in Dept_Tbl are in Tips_Tbl. I need to get all active depts and their most recent tips_id if they have one.
select a.dept_id, b.tips_id
from dept_tbl a, tips_tbl b
where a.status = 'Active'
and a.effdt =
(select max(a_ed.effdt) from dept_tbl a_ed
where a.dept_id = a_ed.dept_id
and a_ed.effdt <= SYSDATE)
and b.dept_id(+) = a.dept_id
and b.effdt =
(select max(b_ed.effdt) from tips_tbl b_ed
where b.dept_id = b_ed.dept_id
and b.tips_id = b_ed.tips_id
and b_ed.effdt <= SYSDATE)
The query only returns records that are in both tables.
Is there a way for outer join to work with effective dates?
Thanks in advance,
RegHere is an example of one solution. Note the use of NVL on both sides of the equal sign since both sides could be NULL in the case of the outer join
with dept_tbl as (select 1 dept_id, SYSDATE effdt FROM DUAL),
tips_tbl as (select 1 dept_id, NULL effdt FROM DUAL)
select a.effdt
from dept_tbl a,
tips_tbl b
where b.dept_id(+) = a.dept_id
and NVL(b.effdt, SYSDATE) = NVL((select max(b_ed.effdt)
from tips_tbl b_ed
where b.dept_id = b_ed.dept_id
and b_ed.effdt <= SYSDATE), SYSDATE);
EFFDT
5/7/2007 3:25:00 PM -
here is the query with Four tables as outer joins
select A.student, B.race, B.sex, B.bdate, trunc(C.cdate-B.bdate)/365.25) "age",
A.randomno, A.randomdt, min(D.vdate), max(D.vdate) E.tdate, E.termrsn
from case A, hist B, ent C, log D, ter E
where A.randomno > 0
and A.del IS NULL
and B.del IS NULL
and B.ndone IS NULL
and B.seq = 0.02
and C.del IS NULL
and c.ndone IS NULL
and D.del IS NULL
and D.ndone IS NULL
and E.del IS NULL
and E.ndoe IS NULL
and average > 0
and (B.student = A.student (+))
and (C.student = A.student (+))
and (E.student = A.student (+))
and (D.student = A.student (+))
group by A.student, B.race, B.sex, B.bdate, trunc(c.cdate-b.bdate)/365.25), A.randomno,
A.randomdt , E.vdate, E.termrsn
=============================================
next query with outer joins but with subqueries in the from clause
select A.student, B.race, B.sex, B.bdate, trunc(C.cdate-B.bdate)/365.25) "age",
A.randomno, A.randomdt, min(D.vdate), max(D.vdate) E.tdate, E.termrsn
from A,
(select race, sex, bdate, student from hist
where del IS NULL and ndone IS NULL and seq = 0.02) B,
(Selcet cdate, student from ent
where del IS NULL and ndone IS NULL ) C,
(Select vdate, student from log
where del IS NULL and ndone IS NULL and average > 0) D,
(Select vdate, termrsn, student from ter
where del IS NULL and ndone IS NULL ) E
(where A.randno >0) and A.del IS NULL
and (A.student = B.Student (+))
and (A.student = C.Student (+))
and (A.student = E.Student (+))
and (A.student = D.Student (+))
group by A.student, B.race, B.sex, B.bdate, trunc(c.cdate-b.bdate)/365.25), A.randomno,
A.randomdt , E.vdate, E.termrsn
This two queries runs fine....but the second query gives correct records(WHICH IS WHAT I AM EXPECTING)
The previous query gives 4 records less.
Does Oracle support outer joins with out subquerys in the from clause or i am doing some thing wrong here(I mean syntax error)
DO I need to write subqueries for every outer join to get the correct record.
If I do outer joins with one or two tables with out subqueries.....I do not have any problem
Thanks
Rama
nullThere are two issues. First Oracle supports outer joins against one table. This means you can do this:
where a.col1 = b.col1(+)
and a.col2 = c.col2(+)
and a.col3 = d.col3(+)
but you can't do this:
where a.col1 = b.col1(+)
and b.col2 = c.col2(+)
because this outer joins to more than one table.
The second issue is that you must outer join to all columns of a table. So you can't do this:
where a.col1 = b.col1(+)
and b.col2 is null
but you can do this:
where a.col1 = b.col1(+)
and 'X' = nvl( b.col2(+), 'X' )
null -
Update On data from multiple Tables
How can i update data from multiple tables(Using a Join)
using a single update statement.Hii
Suppose i have following tables
create table emp_mas(empno number primary key,name varchar2(30));
create table emp_grad(empno number primary key,grade varchar2(1));
SQL> select * from emp_grad;
EMPNO G
100 A
101 A
102 B
SQL> select * from emp_mas;
EMPNO NAME
100 GREG
101 THOMAS
102 SAM
I would like to update information of employee 'GREG' Using a single update statement as
UPDATE (SELECT E.EMPNO,E.NAME,G.GRADE FROM EMP_MAS E,EMP_GRAD G WHERE E.EMPNO=G.EMPNO) J
SET J.NAME='GREG THOMAS',J.GRADE='B' WHERE EMPNO=100
Then i am getting Error message like this.
ERROR at line 2:
ORA-01776: cannot modify more than one base table through a join view
How can i achieve the result
Maybe you are looking for
-
How can I update my Macbook Pro 13inch 10.5.8 to 10.6
I have bought Lion and Mountain Lion came out yesterday. But I wanted to restore my Macbook Pro and put in my Install DVD that came with my Mac. I bought my Macbook Pro in late 2009. When I restored my mac, it gave me tons of updates and the last upd
-
Please help with highest quality export settings...
I have searched the forum and web for hours (no joke!) and I am still trying to figure out what are the best video settings to give me the highest HD quality video in daylight. I am using a Canon HF200 and the latest updated version of FCE. My proble
-
How do I hide my hdd from my desktop?
-
i tried all that but when i connect my iphone to my itunes there is no restore button. why is this happening?
-
Hi just bought a Nokia N79 having problems with the phone memory. A pop up msg keeps popping up say memory full delete data. What Data? I've noticed that any application i enter the memory becomes less. Pls help this is becoming really irritating.