Oracle 8i, left join with conditional
Coming from the MySQL world, I'm trying to do a left join with a
condition:
select c.givenname,c.surname,c.userid,r.letternr
from cand c,responses r
where (c.userid=r.username(+))
and
c.activeprofile=1
No problem whatsoever.
If there is no corresponding "response" in R for a given
candidate, I get a NULL return for R.letternr.
However, there is a flag in R, called "VISIBLE" that I wish to
use to mean "Don't count this entry in R". R.VISIBLE=0 means
that the response is not active/present/visible/valid.
Am I making any sense? :-)
If you don't want to display a row with a null value for
r.letternr when r.visible = 0, then:
SELECT c.givenname,
c.surname,
c.userid,
r.letternr
FROM cand c,
responses r
WHERE c.userid = r.username (+)
AND c.activeprofile = 1
AND r.visible != 0
Or, if you do want to display a row with a null value for
r.letternr when r.visible = 0, then:
SELECT c.givenname,
c.surname,
c.userid,
r.letternr
FROM cand c,
responses r
WHERE c.userid = r.username (+)
AND c.activeprofile = 1
AND r.visible (+) != 0
Similar Messages
-
LEFT JOINING with a string?
I'm viewing code where the person left joined a column with a string.
lu_ods.lookup_type(+) = 'FORX_ORDERPRO_ACTION'Is this just bad coding. Is there any reason why someone would do this?
Doesn't
lu_ods.lookup_type = 'FORX_ORDERPRO_ACTION'work just as well?As Sentinel said, it can change the result if you are left joining between two tables:
with a as (
select 1 num, 'a' col from dual UNION ALL
select 2 num, 'b' col from dual UNION ALL
select 3 num, 'c' col from dual
b as (
select 1 num, 'a' col from dual UNION ALL
select 4 num, 'e' col from dual UNION ALL
select 5 num, 'f' col from dual
select *
from a, b
where a.num(+) = b.num
and a.num(+) = 1;
NUM COL NUM COL
1 a 1 a
5 f
4 e
3 rows selectedWithout the left join on the number, we lose results:
with a as (
select 1 num, 'a' col from dual UNION ALL
select 2 num, 'b' col from dual UNION ALL
select 3 num, 'c' col from dual
b as (
select 1 num, 'a' col from dual UNION ALL
select 4 num, 'e' col from dual UNION ALL
select 5 num, 'f' col from dual
select *
from a, b
where a.num(+) = b.num
and a.num = 1;
NUM COL NUM COL
1 a 1 a
1 rows selected -
JPA Criteria : LEFT JOIN with an AND condition
Hi all,
I have a question regarding JPA criteria.
Here is my JPA query :
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<Email> emailRoot = criteriaQuery.from(Email.class);
criteriaQuery.distinct(true);
Predicate globalCondition = criteriaBuilder.equal(emailRoot.get(Email_.type), EmailType.in);
Predicate responseMandatoryCondition = criteriaBuilder.equal(emailRoot.get(Email_.responseMandatory), true);
Predicate typeCondition = criteriaBuilder.notEqual(emailRoot.join(Email_.emailsOut, JoinType.LEFT).get(Email_.responseType),ResponseType.response);
globalCondition = criteriaBuilder.and(globalCondition, responseMandatoryCondition);
globalCondition = criteriaBuilder.and(globalCondition, typeCondition);
em.createQuery(mainCriteria.where(globalCondition)).getSingleResult();
Here is the result of this query :
SELECT DISTINCT email0_.ID AS col_0_0_
FROM EMAIL email0_
LEFT OUTER JOIN email emailso1_ ON email0_.ID = emailso1_.ID_EMAIL_IN
WHERE email0_.TYPE = 'in'
AND email0_.RESPONSE_MANDATORY = true
AND emailso1_.RESPONSE_TYPE <> 'response'
LIMIT 0 , 30
And here is the request I needed :
SELECT DISTINCT email0_.ID AS col_0_0_
FROM EMAIL email0_
LEFT OUTER JOIN email emailso1_ ON email0_.ID = emailso1_.ID_EMAIL_IN AND emailso1_.RESPONSE_TYPE <> 'response'
WHERE email0_.TYPE = 'in'
AND email0_.RESPONSE_MANDATORY = true
LIMIT 0 , 30
As you can see I need to check if the RESPONSE_TYPE is equals on the same line that the LEFT OUTER JOIN. Does anybody know how to write such an JPA criteria query ?
Thanks ,
Louis
Edited by: user13105928 on 17 févr. 2011 03:06You 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 -
Whats the alternate for left join with using IN operator in the where claus
My senario...
Select t1.f1, t1.f2, t2.f3
From t1, t2
Where ...
t1.f1> (+) IN t2.f1
...alternate query..
Select t1.f1, t1.f2, t2.f3
From t1, t2
Where ...
(t1.f1> IN t2.f1 or 1=1)
...will the alternate query satisfy the above senario..?I'm not quite sure I've understood what the OP's question is, but will ANSI syntax deal with it?
SQL> ed
Wrote file afiedt.buf
1 with a as (select 1 as num, 2 as data from dual union all
2 select 2 as num, 3 as data from dual union all
3 select 3 as num, 4 as data from dual),
4 b as (select 1 as num, 5 as data from dual union all
5 select 3 as num, 7 as data from dual)
6 -- END OF TEST DATA
7 select a.num, a.data, b.data
8* from a LEFT OUTER JOIN b ON (a.num = b.num AND b.num IN (3))
SQL> /
NUM DATA DATA
3 4 7
1 2
2 3
SQL> -
How To do a LEFT JOIN in Oracle 8i
Hello
To explain my problem in a simple way: I have two tables, PROVA1 and PROVA2. Both have a primary key with two fields (CHIAVE1 amd CHIAVE2, numbers).
I have to search for all records in A but not in B. In Oracle 8 LEFT JOIN is not available and I do not Know how doing it.
I have read something about + operator, but I am not sure. What about:
Select * from PROVA1 P1, PROVA2 P2 where
(P1.CHIAVE1(+) = P2.CHIAVE1) and
(P1.CHIAVE2(+) = P2.CHIAVE2) and
(P2.CHIAVE1 IS NULL) and
(P2.CHIAVE2 IS NULL)
Thank you
Garetano RecchiGaetano, you're really close.
The (+) sign goes near to the table where the records can be missed. In your example near P2 columns.
Select *
from PROVA1 P1, PROVA2 P2
where P1.CHIAVE1 = P2.CHIAVE1(+)
and P1.CHIAVE2 = P2.CHIAVE2(+)
and P2.CHIAVE1 IS NULL
and P2.CHIAVE2 IS NULLYou can anyway use the minus operator
Select *
from PROVA1
where (chiave1,chiave2) in (select chiave1, chiave2 from prova1 minus select chiave1, chiave2 from prova2);If you want to download a free book (in italiano) for oracle beginners go to
My Oracle-related blog
Max -
Inconsistent results with ANSI LEFT JOIN on 9iR2
Is this a known issue? Is it solved in 10g?
With the following data setup, I get inconsistent results. It seems to be linked to the combination of using LEFT JOIN with the NULL comparison within the JOIN.
create table titles (title_id int, title varchar(50));
insert into titles values (1, 'Red Book');
insert into titles values (2, 'Yellow Book');
insert into titles values (3, 'Blue Book');
insert into titles values (4, 'Orange Book');
create table sales (stor_id int, title_id int, qty int, email varchar(60));
insert into sales values (1, 1, 1, '[email protected]'));
insert into sales values (1, 2, 1, '[email protected]');
insert into sales values (3, 3, 4, null);
insert into sales values (3, 4, 5, '[email protected]');
SQL> SELECT titles.title_id, title, qty
2 FROM titles LEFT OUTER JOIN sales
3 ON titles.title_id = sales.title_id
4 AND stor_id = 3
5 AND sales.email is not null
6 ;
TITLE_ID TITLE QTY
4 Orange Book 5
3 Blue Book
1 Red Book
2 Yellow Book
SQL>
SQL> SELECT titles.title_id, title, qty
2 FROM titles LEFT OUTER JOIN sales
3 ON titles.title_id = sales.title_id
4 AND 3 = stor_id
5 AND sales.email is not null;
TITLE_ID TITLE QTY
2 Yellow Book 1
4 Orange Book 5
3 Blue Book
1 Red Book
It seems to matter what order I specify the operands stor_id = 3, or 3 = stor_id.
In the older (+) environment, I would understand this, but here? I'm pretty sure most other databases don't care about the order.
thanks for your insight
KevinDon't have a 9i around right now to test ... but in 10 ...
SQL> create table titles (title_id int, title varchar(50));
Â
Table created.
Â
SQL> insert into titles values (1, 'Red Book');
Â
1 row created.
Â
SQL> insert into titles values (2, 'Yellow Book');
Â
1 row created.
Â
SQL> insert into titles values (3, 'Blue Book');
Â
1 row created.
Â
SQL> insert into titles values (4, 'Orange Book');
Â
1 row created.
Â
SQL> create table sales (stor_id int, title_id int, qty int, email varchar(60));
Â
Table created.
Â
SQL> insert into sales values (1, 1, 1, '[email protected]');
Â
1 row created.
Â
SQL> insert into sales values (1, 2, 1, '[email protected]');
Â
1 row created.
Â
SQL> insert into sales values (3, 3, 4, null);
Â
1 row created.
Â
SQL> insert into sales values (3, 4, 5, '[email protected]');
Â
1 row created.
Â
SQL> SELECT titles.title_id, title, qty
2 FROM titles LEFT OUTER JOIN sales
3 ON titles.title_id = sales.title_id
4 AND stor_id = 3
5 AND sales.email is not null
6 ;
Â
TITLE_ID TITLE QTY
4 Orange Book 5
3 Blue Book
1 Red Book
2 Yellow Book
Â
SQL>
SQL> SELECT titles.title_id, title, qty
2 FROM titles LEFT OUTER JOIN sales
3 ON titles.title_id = sales.title_id
4 AND 3 = stor_id
5 AND sales.email is not null;
Â
TITLE_ID TITLE QTY
4 Orange Book 5
3 Blue Book
1 Red Book
2 Yellow Book
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options -
Wrap a store procedure which include "Left Join"
In 9i, i write a store procedure :
CREATE OR REPLACE PROCEDURE
get_code(STATION_NUM IN VARCHAR2,COMMAND1 OUT VARCHAR2) AS
BEGIN
SELECT A.*, B.XX
FROM A LEFT JOIN B ON A.ID = B.ID
WHERE A.STATION_NUM = STATION_NUM
END;
AND, I wrap this store procedure, it show error message:
PSU(103,1,50,41):Encountered the symbol "LEFT" when expecting one of the following:
, ; for group having intersect minus order start union where
connect
Why i can run this store procedure, but can't wrap it?
my oracle is 9.2.0.1.0Replace Left Join with "," and put "(+)" symbol after A.ID
Regds,
Balaji -
Doubt Regarding LEFT JOIN Operation
Hello,
With the Old syntax in place it is easier for find that which join is performed on which table ...
But with the new syntax ..m getting bit confused when INNER & LEFT JOINS used together ...
Below is the FROM clause ...please explain .. how this JOIN is gonna take place ..& which table is Left joined with whom...
FROM VW_TRANS_MANAGER_SALES ref_1
LEFT OUTER JOIN VW_AGREE_ASSIGN_TAG_DTLS ass
ON Trim(ass.agreement_id) = trim(ref_1.AGREEMENT_ID)
LEFT OUTER JOIN VW_CURENT_RATING_DTLS CURR
ON (ref_1.company_code =curr.COMPANYCODE)
LEFT OUTER JOIN VW_PREV_RATING_DETAILS PREV
ON (ref_1.company_code =prev.COMPANYCODE)
LEFT OUTER JOIN CRMADMIN.CO_MA_COMPANY_CONTACTS cont
ON (ref_1.CLIENT_CONTACT_ID= cont.CONTACT_ID)
LEFT OUTER JOIN CRMADMIN.COR_CRM_MST_CITY city
ON (city.City_id= cont.City_id))
---------------------------------------------------------------------------------------------------------------------------------------Hi,
Aijaz Mallick wrote:
Yeah .. but was a bit Confusing as u used 2 columns in each table....
Doesn't it depends on the joining condition we use ... lets Say ..if i use an INNER join after 2 LEFT join ... & m doing an that inner join with the first SOURCE table .... then will it perform a join on the resultset on the previous Joins...???Sorry, I'm not sure what you're asking.
Please post a specific example of a join that you don't understand, or some results that you don't know how to get.
Use commonly available tables (like those in the scott or hr schemas) or post your own CREATE TABLE and INSERT statements.
Using ANSI syntax, the results are as if the joins were done in the order they appear in the FROM clause (even though the optimizer may not actually do them in that order).
For example, in the querry below, the inner-join between emp and salgrade is done first, then the outer join with dept is done to that result set.
SELECT d.dname
, e.ename
, g.losal
FROM scott.emp e
JOIN scott.salgrade g ON e.sal BETWEEN g.losal
AND g.hisal
RIGHT OUTER JOIN scott.dept d ON e.deptno = d.deptno
;Output:
DNAME ENAME LOSAL
ACCOUNTING CLARK 2001
ACCOUNTING MILLER 1201
ACCOUNTING KING 3001
RESEARCH FORD 2001
RESEARCH SCOTT 2001
RESEARCH JONES 2001
RESEARCH ADAMS 700
RESEARCH SMITH 700
SALES BLAKE 2001
SALES ALLEN 1401
SALES MARTIN 1201
SALES WARD 1201
SALES JAMES 700
SALES TURNER 1401
OPERATIONSNotice that the OPERATIONS department, which has no matches in the other tables, is still included because of the outer join.
If you want to have the joins done in a different order, you can explicitly join some table first, either in a sub-query or just by grouping joins within parentheses in the same FROM clause, as in the query below (which produces the same results as the query above):
SELECT d.dname
, e.ename
, g.losal
FROM scott.dept d
LEFT OUTER JOIN ( -- Join the following tables first:
scott.emp e
JOIN scott.salgrade g ON e.sal BETWEEN g.losal
AND g.hisal
) ON d.deptno = e.deptno
; -
T-sql 2008 r2 cte join with a second table
In a sql server 2008 r2 database, I am trying to take the results of the cte called sProgram and join the results with the Person table in the query. The syntax of the join looks ok, however the results of the join are incorrect. The joined results are null
when there is really data that can be joined. Can you tell me how to join the query so the results of the sprogram cte will be joined to the Person table correctly?
1. Here is the query:
;with sProgram as (
SELECT [personID],[attributeID],[value], [date],ROW_NUMBER() OVER(Partition by[personID] ORDER BY [personID],[date] DESC) rn
FROM [dbo].[CustomS]
where [attributeID] in ('562','563','564','565')
,programSelect as
select [personID],[attributeID],[value],[date]
from sProgram
where rn=1
SELECT person.personID
, cs562.personID
, cs562.attributeID
, cs562.value
, cs562.[date]
, cs563.personID
, cs563.attributeID
, cs563.value
, cs563.[date]
, cs564.personID
, cs564.attributeID
, cs564.value
, cs564.[date]
, cs565.personID
, cs565.attributeID
, cs565.value
, cs565.[date]
FROM Person cs562
join programSelect on cs562.personID = Person.personID and cs562.attributeID= '562'
left join programSelect cs563 on cs563.personID = cs562.personID and cs563.[Date] = cs562.[date] and cs563.attributeID ='563'
left join programSelect cs564 on cs564.personID = cs563.personID and cs564.[date] = cs563.[Date] and cs564.attributeID ='564'
left join programSelect cs565 on cs565.personID = cs564.personID and cs565.[Date] = cs564.[date] and cs565.attributeID ='565'
GROUP BY
person.personID
, cs562.personID
, cs562.attributeID
, cs562.value
, cs562.[date]
, cs563.personID
, cs563.attributeID
, cs563.value
, cs563.[date]
, cs564.personID
, cs564.attributeID
, cs564.value
, cs564.[date]
, cs565.personID
, cs565.attributeID
, cs565.value
, cs565.[date]
2. here is the CustomS table with some data.
SELECT [CustomSID]
,[personID]
,[attributeID]
,[value]
,[date]
FROM [dbo].[CustomS]
where personID=7170
and attributeID in (562,563,564,565)
order by date desc
CustomSID personID attributeID value [date]
262490684 7170 562 GA 2013-08-14 07:26:00
262490683 7170 565 05/23/2014 2013-08-14 07:26:00
262490682 7170 563 Acd 2013-08-14 07:26:00
262490681 7170 564 08/14/2013 2013-08-14 07:26:00
251784 7170 564 09/06/2007 2007-09-08 00:00:00
250029 7170 562 MA 2007-09-08 00:00:00
248287 7170 563 asp 2007-09-08 00:00:00
251785 7170 564 09/07/2006 2006-09-08 00:00:00
248286 7170 563 asp 2006-09-08 00:00:00
250028 7170 562 MA 2006-09-08 00:00:00
251783 7170 564 09/06/2006 2006-09-06 00:00:00
249367 7170 562 LA 2006-09-06 00:00:00
248285 7170 563 asp 2006-09-06 00:00:00
3. Here is the table definition:
here is the table definition:
alter TABLE [dbo].[CustomS](
[CustomSID] [int] IDENTITY(1,1) NOT NULL,
[personID] [int] NOT NULL,
[attributeID] [int] NOT NULL,
[value] [varchar](256) NULL,
[date] [smalldatetime] NULL,Partition by CustomsID will didvide data into groups based on CustomsID value.
ROW_NUMBER will generate sequence numbers over the group and when you look for rn=1 you'll get record with latest group
Also from what I understand you need just this instead of all those left joins
;with sProgram as (
SELECT [personID],[attributeID],[value], [date],
ROW_NUMBER() OVER(Partition by [personID],[attributeID] ORDER BY [date] DESC) rn
FROM [dbo].[CustomS]
where [attributeID] in ('562','563','564','565')
SELECT p.personID
, pr.personID , pr.attributeID , pr.value , pr.[date]
, pr1.*
FROM Person p
left join sProgram pr
on pr.personID = p.personID
and pr.Rn = 1
and attributeID = '562'
outer apply (SELECT personID,
max(case when attributeID = '563' then attributeID end) as AttrID563,
max(case when attributeID = '563' then value end) as Value563,
max(case when attributeID = '563' then date end) as date563,
max(case when attributeID = '564' then attributeID end) as AttrID564,
max(case when attributeID = '564' then value end) as Value564,
max(case when attributeID = '564' then date end) as date564,
max(case when attributeID = '565' then attributeID end) as AttrID565,
max(case when attributeID = '565' then value end) as Value565,
max(case when attributeID = '565' then date end) as date565
where personID = p.PersonID
and [date] = pr.[date]
and Rn = 1
group by personId)pr1
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
3 tables with left joins - bug?
Hello,
i am making query where i encounter problem with left join in oracle. I am using oracle 10g and i prepare simple test case.
he is testing tables and datas - really simple i think:
drop table t1;
drop table t2;
drop table t3;
create table t1 (a number not null);
create table t2 (a number, b number);
create table t3 (b number);
insert into t3 values (1);
insert into t3 values (2);
insert into t3 values (3);
insert into t1 (a) values (1);
insert into t2 (a,b) values (1,1);
insert into t1 (a) values (2);
insert into t2 (a,b) values (2, null);
insert into t1 (a) values (3);
insert into t1 (a) values (4);
insert into t2 (a,b) values (4,1);
insert into t1 (a) values (5);
insert into t2 (a,b) values (5,3);
and now query with left joins:
select
t1.a
, t2.a, t2.b
, t3.b
from
t1, t2, t3
where
t1.a = t2.a (+)
and t2.b = t3.b (+)
and t3.b is null
order by t1.a
i get two rows as result:
A A_1 B B_1
2 2 null null
3 null null null
i expect these rows but when i change my query - i dont want get back t3.b column:
select
t1.a
, t2.a, t2.b
/* , t3.b*/
from
t1, t2, t3
where
t1.a = t2.a (+)
and t2.b = t3.b (+)
and t3.b is null
order by t1.a
i get only one row
A A_1 B
2 2 null
My question is simple how can i only by changing columns getting back change number of returned rows? I must say i dont expect these result i expect two rows again.
Thanks for help.BluShadow wrote:
I think I know what you are getting at.
By testing for null on t3.b when you aren't selecting the column, you are enforcing oracle to perform the join through t2 onto t1, but Oracle can't join because t2 has no matching row (although it's outer joined to t1) and therefore, for the one row it can't actually determine if t3.b is null or not, so that row can't match the conditions in a "true" sense and be displayed. If you select the column then oracle can test its nullness ok. (Perhaps this is a bug, I don't know, it's just how I know it works)If you get different results only by changing the projection part of the query this is a bug and nothing else. I can't reproduce using Oracle 10g XE, I get in both cases shown the expected two rows.
What versions are you using to test this?
SQL>
SQL> select * from v$version
2 where rownum <= 1;
BANNER
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
SQL>
SQL> drop table t1 purge;
Table dropped.
SQL> drop table t2 purge;
Table dropped.
SQL> drop table t3 purge;
Table dropped.
SQL>
SQL> create table t1 (a number not null);
Table created.
SQL> create table t2 (a number, b number);
Table created.
SQL> create table t3 (b number);
Table created.
SQL>
SQL> insert into t3 values (1);
1 row created.
SQL> insert into t3 values (2);
1 row created.
SQL> insert into t3 values (3);
1 row created.
SQL>
SQL> insert into t1 (a) values (1);
1 row created.
SQL> insert into t2 (a,b) values (1,1);
1 row created.
SQL>
SQL> insert into t1 (a) values (2);
1 row created.
SQL> insert into t2 (a,b) values (2, null);
1 row created.
SQL>
SQL> insert into t1 (a) values (3);
1 row created.
SQL>
SQL> insert into t1 (a) values (4);
1 row created.
SQL> insert into t2 (a,b) values (4,1);
1 row created.
SQL>
SQL> insert into t1 (a) values (5);
1 row created.
SQL> insert into t2 (a,b) values (5,3);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> select t1.a
2 , t2.a, t2.b
3 , t3.b
4 from
5 t1 left outer join t2 on (t1.a = t2.a)
6 left outer join t3 on (t2.b = t3.b)
7 where t3.b is null
8 order by t1.a;
A A B B
2 2
3
SQL>
SQL> select t1.a
2 , t2.a, t2.b
3 -- , t3.b
4 from
5 t1 left outer join t2 on (t1.a = t2.a)
6 left outer join t3 on (t2.b = t3.b)
7 where t3.b is null
8 order by t1.a;
A A B
2 2
3
SQL>Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
Testing for IS NOT NULL with left join tables
Dear Experts,
The query is showing the NULL rows in the query below....
Any ideas, advice please? tx, sandra
This is the sql inspector:
SELECT O100321.FULL_NAME_LFMI, O100321.ID, O100404.ID, O100321.ID_SOURCE
, O100404.NAME, O100321.PERSON_UID, O100404.PERSON_UID, O100404.VISA_TYPE
FROM ODSMGR.PERSON O100321
, ODSMGR.VISA O100404
WHERE ( ( O100321.PERSON_UID = O100404.PERSON_UID(+) ) ) AND ( O100404.VISA_TYPE(+) IS NOT NULL )Hi Everyone,
I am understanding alot of what Michael and Rod wrote.... I am just puzzled over the following:
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 -
Need assistance with conditional join in SQL
Hi All -
I need to ask for help with this query:
Create table user_tab_col_test (table_name varchar2(30), column_name varchar2(30), data_type varchar2(30));
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column1', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column2', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column3', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column4', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column1', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column2', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column3', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column4', 'varchar2')
Commit;
Create table all_cons_columns_test (table_name varchar2(30), column_name varchar2(30), constraint_name varchar2(30))
Insert into all_cons_columns_test (table_name, column_name, constraint_name) values ('table1', 'column1', 'primary')
Insert into all_cons_columns_test (table_name, column_name, constraint_name) values ('table1', 'column1', 'secondary')
Commit;
This is my query and the current result:
Select u.table_name, u.column_name, c.constraint_name
From user_tab_col_test u
Left outer join all_cons_columns_test c
On ( u.table_name = c.table_name
AND U.COLUMN_NAME = C.COLUMN_NAME
AND C.CONSTRAINT_NAME IN ('primary'))
order by U.table_name, U.COLUMN_NAME;
TABLE_NAME COLUMN_NAME CONSTRAINT_NAME
table1 column1 primary
table1 column2
table1 column3
table2 column1
table2 column2
Three questions:
1) I only want to return results where table_name = 'table1'. I can't seem to get this to work.
2) Is my query proper and is this the best way to return my desired results? I.e. I want all columns from user_tab_col_test and I only want to display the constraint_name from all_cons_columns_test if the constraint_name = 'primary'.
3) Will the synatx be the same if I need to join a third table to all_cons_columns_test?
Any advice/suggestions are much appreciated -
john
Edited by: user703358 on Jan 11, 2013 8:57 PM
Edited by: user703358 on Jan 11, 2013 9:48 PMHi,
user703358 wrote:
... ALL_CONSTRAINTS_TEST joins to ALL_CONS_COLUMNS_TEST on TABLE_NAME and CONSTRAINT_NAME. If you adapt this to use the data dictionary views ALL_CONSTRAINTS and ALL_CONS_COLUMNS, then rememeber to join on the OWNER column, also.
Ultimately I want to use ALL_CONSTRAINTS_TEST.CONSTRAINT_TYPE = 'P' in my WHERE clause, instead of C.CONSTRAINT_NAME IN (primary), only because the constraint_type is a more definitive attribute than just the name of the constraint.
I tried something like the query below but I'm getting
ORA-00904: "U"."COLUMN_NAME": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 8 Column: 16Errors like that are caused by trying to mix old join syntax with ANSI join syntax in the same query.
SELECT u.table_name
, u.column_name
, C.CONSTRAINT_NAME
FROM USER_TAB_COL_TEST U,
ALL_CONSTRAINTS_TEST ACAbove is an example of an old-style join: there is a comma between the table names, and the join conditions are included in the WHERE clause below.
LEFT OUTER JOIN all_cons_columns_test c
ON u.table_name = c.table_name
AND U.COLUMN_NAME = C.COLUMN_NAMEThis is an example of an ANSI-style join: the keyword JOIN appears between the table names, and the join conditions are right here, after the keyword ON.
WHERE U.TABLE_NAME = 'table1'
AND C.TABLE_NAME = AC.TABLE_NAME
AND C.CONSTRAINT_NAME = AC.CONSTRAINT_NAME
and AC.CONSTRAINT_TYPE = 'P'
ORDER BY u.table_name
, u.column_name
;While it is possible to use both join styles in the same query, it's a really bad idea. I suggest always using ANSI syntax, especially for outer joins, but whatever style you choose, use it consistently for all joins in the same query.
In this case, you want an inner join between tablec c and ac, so the ANSI syntax would be something like
FROM u LEFT OUTER JOIN c ON ... JOIN ac ON ...In this case, it's very important that the inner join between c and ac is done before the outer join with u. How can you make sure that happens? Well, if you have an arithmetic expression such as
12 * 5 - 1and you want to make sure the subtraction of 1 from 5 takes place before the multiplication by 12, what do you do? You can add parentheses:
12 * ( 5 - 1 )In ANSI join syntax, you can use parentheses the same way:
FROM u LEFT OUTER JOIN ( c ON ... JOIN ac ON ... )or, in full:
SELECT u.table_name
, u.column_name
, c.constraint_name
, ac.constraint_type
FROM user_tab_col_test u
LEFT OUTER JOIN (
all_cons_columns_test c
JOIN all_constraints_test ac
ON ac.table_name = c.table_name
AND ac.constraint_name = c.constraint_name
ON u.table_name = c.table_name
AND u.column_name = c.column_name
AND ac.constraint_type = 'P'
WHERE u.table_name = 'table1'
ORDER BY u.table_name
, u.column_name
;Output:
TABLE_NAME COLUMN_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
table1 column1 primary P
table1 column2
table1 column3
table1 column4 -
Simple left-join doesn't work. error with: Unknown column
Hi guys,
can anyone assist on this simple query:
SELECT D.* , SUM( TD.debit_amount), SUM(TAP.amount)
FROM debit AS D, transaction_debit AS TD
LEFT JOIN TA_PAYMENTS AS TAP ON D.debit_id=TAP.debit_id
WHERE D.debit_id = TD.debit_id
GROUP BY TD.debit_idThe error message I receive is:
#1054 - Unknown column 'D.debit_id' in 'on clause' I'm trying to relate 3 tables (debit, transactions-debit and ta_payment), some payment may not exist so therefore i use left join.
thanks for any pointersSELECT D.* , SUM( TD.debit_amount), SUM(TAP.amount)
FROM debit AS D, transaction_debit AS TD
LEFT JOIN TA_PAYMENTS AS TAP ON
D.debit_id=TAP.debit_id
WHERE D.debit_id = TD.debit_id
GROUP BY TD.debit_id
I'm trying to relate 3 tables (debit,
transactions-debit and ta_payment), some payment may
not exist so therefore i use left join.You are trying to mix "the older join syntax", with the join condition in the where-clause, with "the new join syntax", with the join condition in the "on-clause", which I think is causing trouble.
How about using only "the new syntax":
SELECT D.* , SUM( TD.debit_amount), SUM(TAP.amount)
FROM debit AS D JOIN transaction_debit AS TD on (D.debit_id = TD.debit_id)
LEFT JOIN TA_PAYMENTS AS TAP ON (D.debit_id=TAP.debit_id)
GROUP BY TD.debit_idDoes that help? -
How do u write a LEFT JOIN in Oracle Developer Forms 6i?
I have 2 tables:PERSDET and EMP_MEDICAL_LTA
PERSDET has EMP_ID,EMP_NAME n some other fields.
EMP_MEDICAL_LTA has EMP_ID,FIELD1,FIELD2 n some other fields
EMP_ID is the common field between these 2 tables.
My requirement is to display all the EMP_NAME,that is-EMP_NAME which have field1 and field2 entries, as well as EMP_NAME which donot have field1 and field2 entries. I thought of using a LEFT join for this operation. The query I used is as follows:
SELECT PERSDET.emp_id,PERSDET.emp_name,EMP_MEDICAL_LTA.field1,EMP_MEDICAL_LTA.field2 into emp_id,emp_name,m,l FROM
PERSDET LEFT JOIN EMP_MEDICAL_LTA ON PERSDET.emp_id=EMP_MEDICAL_LTA.EMP_ID
where <some other joining conditions>;
This query worked perfectly fine when I tried it running on the SQL prompt, but when I wrote it in a cursor in Oracle Forms 6i, it gave an error on the LEFT keyword!
Is there any substitute for LEFT JOIN in Oracle Forms 6i???
Please help!
Thanks!hi,
yes it is very much possible. and you query willbe as follows;
SELECT PERSDET.emp_id,PERSDET.emp_name,EMP_MEDICAL_LTA.field1,EMP_MEDICAL_LTA.field2 into emp_id,emp_name,m,l FROM
PERSDET where PERSDET.emp_id=EMP_MEDICAL_LTA.EMP_ID(+)
where <some other joining conditions>;
yse, you just have to use (+) symble on the side where the join condition is not fullfilled. the 2 possibilites for your qury is PERSDET.emp_id=EMP_MEDICAL_LTA.EMP_ID(+)
PERSDET.emp_id(+)=EMP_MEDICAL_LTA.EMP_ID
you can execute both and suit your self.
MohibMaan. -
Need help with conditional join....
Table 1 - Student
ID# Name
1 # A
2 # B
3 # C
4 # D
Table2 - Marks
ID Marks Display
1 # 10 # Y
1 # 20 # Y
1 # 14 # N
2 # 12 # N
2 # 13 # N
3 # 12 # Y
Result...Need query to do this?..
Want to join above two tables and display marks as X when there is no ID in table marks or there is ID but all marked with display as 'N'...if there is one or more
marked with Y then display with marks..
I am using oracle 11i.
ID NAme Marks
1 # A # 10
1 # A # 20
2 # B # X
3 # C # 12
4 # D # XOr, using ANSI join syntax:
with Table1 as (
select 1 id,'A' name from dual union all
select 2,'B' from dual union all
select 3,'C' from dual union all
select 4,'D' from dual
Table2 as (
select 1 id,10 marks,'Y' display from dual union all
select 1,20,'Y' from dual union all
select 1,14,'N' from dual union all
select 2,12,'N' from dual union all
select 2,13,'N' from dual union all
select 3,12,'Y' from dual
-- end of on-the-fly data sample
select t1.id,
name,
nvl(to_char(marks),'X') marks
from Table1 t1
left join
Table2 t2
on (
t2.id = t1.id
and
display = 'Y'
order by id
ID NAME MARKS
1 A 10
1 A 20
2 B X
3 C 12
4 D X
SQL> SY.
Maybe you are looking for
-
Changed computers from XP to windows 7. I was using Adobe Photoshop 4.0 from a floppy. I can't load it on my new computer so I bought elements 12. It doesn't allow me to alter the layers I created? Please advise.
-
Error while opening systemproperties it say reinstall UCM without root uses
UCM 11g gives error while opening Systemproperties applet, it says UCM is installed by root user reinstall it without root user. We have installed the UCM from a non root user however we are getting this error and non of the applet is opening which a
-
Error while generating material classification data source in R/3
Hi, I am trying to generate the data source for material classification in R/3. I have created the data source entry in CTBW and added the classification fields required for the data source. But while generating the data source i am getting an error,
-
How can I turn off a specific keyboard shortcut?
We use a 3rd party vendor's platform to create/maintain content on our school website. When writing stories, I often use standard editing shortcuts (such as command-B for bold, command-I for italic, etc.). But when I hit command-B in the new FF 4.0 r
-
ARW Unsupported By iPhoto???
So yesterday I purchased a new digital camera (Sony NEX-C3) and after the battery charged (5 hours later) I set up the camera and formatted the memory card and started shooting some pictures. I took about 10 pictures and plugged the camera into my co