Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

left join structure 1

Status
Not open for further replies.

chmdguy2

Technical User
Nov 21, 2003
10
0
0
US
I've been having a problem with the structure of a simple 'no-match' query. For example, I have two tables... Table 1 and Table 2. I want to pull all the records from table 1 that are not in table two.

Table 1 Table 2
Id Id

Here's what I've written, but I'm a bit stuck..
--------------------------------
select distinct
Table1.Id,
Table2.Id
from Table1
Table2
left join Table1 on Table2.Id = Table1.Id
where Table2.ID is null;
-------------------------------------

I'm obviously doing something wrong... Can someone please help! Thanks so much!




 
yeah, you don't use both methods (listing the tables and using JOIN syntax)

just JOIN them

[tt]select distinct
Table1.Id
, Table2.Id
from Table1
left outer
join Table2
on Table1.Id = Table2.Id
where Table2.ID is null[/tt]

if Table1.Id is the primary key, you don't really need DISTINCT

and you don't need Table2.Id in the SELECT list, because you know it's gonna be null

;-)


rudy
 
There are fundamentally 2 ways to accomplish this,
and it looks like you have attempted to use both approaches at once. The preferred (newer) method is :

select T1.*
from Table1 T1
left outer join Table2 T2
on T1.[ID] = T2.[ID]
where T1.[ID] is not null
and T2.[ID] is null

The older, Oracle-like method is :

select T1.*
from Table1 T1, Table2 T2
where T1.[ID] *= T2.[ID]
 
i'm surprised to see you describe the older microsoft outer join syntax as "oracle-like"

in sql server, it's [tt]T1.[ID] *= T2.[ID][/tt]

in oracle, it's [tt]T1.[ID](+) = T2.[ID][/tt]


rudy


 
I believe that using EXISTS is a little more efficient as processing stops as soon as a matching row is found.

Code:
SELECT DISTINCT t1.Id
FROM Table1 t1
WHERE NOT EXISTS(SELECT 1 FROM Table2 t2
                 WHERE t1.Id = t2.Id)

--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
ordinarily, yes, angel, that is correct, except that the example you give is a correlated subquery, which is usually executed by the optimizer as a join anyway

the true test would be to take the real life case, code it both ways, and do an EXPLAIN

;-)
 
I have a question related to this problem:

I have a table with weekending dates and facilities that have submitted reports

I also have a table listing every facility there is.

is there an SQL statement where i can create a query listing what facilites have not submitted reports for each weekending date?


it seems to be like a no_match query, but specific to the weekending column rather than an entire table

Thanks if you can help!
 
yes, join the facility table to the reports table, with a WHERE condition for the range of weekending dates you're interested in, GROUP BY facility, and then say

HAVING COUNT(DISTINCT weekendingdate) < somenumber

where the number is how many weekending dates are in the range specified


rudy
 
Thanks,I think I understand, but can you give me an example of an entire statement, cuz im a rookie, and it still isnt working properly....heres a beter picture of what my tables look like

ActivityWeekly
---------------
Weekending | Facility | ect
____________________________
*all columns may include multiple entires of same date & facitliy


Facilities
------------
Facility | ect


I'd like to use all dates that are in the database instead of counting and specifing

thanks so much for your help
 

[tt]select Facility
from Facilities
inner
join ActivityWeekly
on Facilities.Facility
= ActivityWeekly.Facility
group
by Facility
having count(distinct Weekending)
< ( select count(distinct Weekending)
from ActivityWeekly ) [/tt]
 
Thanks I really apreciate all the help, but i am getting a syntax error you see any reason why...i dont :(
 
no, i don't either

give me a hint

what was the error?

 
It gave me a snytax error starting at the fist count,

BTW im using SQL view in MS Access...does it support all functionalities of SQL?

Ryan
 
access does not support COUNT DISTINCT

or, at least it didn't in access 97, not sure about later versions

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top