473,472 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Two Tables with Left Outer Join & Where Clause

Hello,

I'm trying to link two tables... one for Employees and the other for
Timecards

I need to get a list of employees that do not have timecards on an
SPECIFIC DATE

I tried the follonwing

SELECT Employess.EmployeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.EmployeeID =
Timecards.lmpEmployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'

But it doesn't work. However, when I comment the date condition out
(lmpTimecardDate = '10/24/2007') it works all right but It's not what
I need

Another interesting point... if I use the following query... it works
all right

SELECT Employess.EmployeeID
FROM Employees
WHERE Employees.EmployeeID not in (select Timecards.EmployeeID from
Timecards
where TimecardDate = '10/24/2007')

I'd like to be able to use the Left Outer Join option.... Am I doing
something wrong?... or is it that if It doesn't like the condition I'm
usgin in the WHERE clause (TimecardDate = '10/24/2007')

Thanks for your help

Pablo

Oct 29 '07 #1
3 19213
<pb*******@hotmail.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
Hello,

I'm trying to link two tables... one for Employees and the other for
Timecards

I need to get a list of employees that do not have timecards on an
SPECIFIC DATE

I tried the follonwing

SELECT Employess.EmployeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.EmployeeID =
Timecards.lmpEmployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'

But it doesn't work. However, when I comment the date condition out
(lmpTimecardDate = '10/24/2007') it works all right but It's not what
I need

Another interesting point... if I use the following query... it works
all right

SELECT Employess.EmployeeID
FROM Employees
WHERE Employees.EmployeeID not in (select Timecards.EmployeeID from
Timecards
where TimecardDate = '10/24/2007')

I'd like to be able to use the Left Outer Join option.... Am I doing
something wrong?... or is it that if It doesn't like the condition I'm
usgin in the WHERE clause (TimecardDate = '10/24/2007')

Thanks for your help

Pablo
Put any outer (non-preserved) table references into the ON clause:

SELECT Employess.EmployeeID
FROM Employees
LEFT OUTER JOIN Timecards
ON Employees.EmployeeID = Timecards.lmpEmployeeID
AND lmpTimecardDate = '20071024'
WHERE lmpEmployeeID is NULL ;

--
David Portas
Oct 29 '07 #2
(pb*******@hotmail.com) writes:
SELECT Employess.EmployeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.EmployeeID =
Timecards.lmpEmployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'

But it doesn't work. However, when I comment the date condition out
(lmpTimecardDate = '10/24/2007') it works all right but It's not what
In addition to David's post, here is what is happening:

The FROM ... LEFT JOIN operators define a table that includes all rows
in the outer table, Employees in this case. This table includes the columns
from the Timecards table, but for the employees there there is no timecard,
all columns have NULL. Which you apparently have understood, since you
the condition "lmpEmployeeID IS NULL". But then there is a lapse, and you
filter lmpTimecardDate despite it is not likely that there is a row in
Timecards where the date is non-NULL and the employee ID is NULL. (At least
one would hope so!) Moving the date condition to the ON clause addresses
the issue, as it now will be part of the condition that builds the
table that is then filtered by WHERE.

Personally, I would prefer to write this query with NOT EXISTS:

SELECT E.Employee
FROM Employees E
WHERE NOT EXISTS (SELECT *
FROM Timecards T
WHERE E.EmployeeID = T.lmpEmployeeID
AND T.lmpTimecardDate = '20071014')

Simply because this clearly express what this is all about.

And I would also use a date format that is safe from misinterpretations.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Oct 29 '07 #3
On Oct 30, 9:22 am, Erland Sommarskog <esq...@sommarskog.sewrote:
(pbassu...@hotmail.com) writes:
SELECT Employess.EmployeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.EmployeeID =
Timecards.lmpEmployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'
But it doesn't work. However, when I comment the date condition out
(lmpTimecardDate = '10/24/2007') it works all right but It's not what

In addition to David's post, here is what is happening:

The FROM ... LEFT JOIN operators define a table that includes all rows
in the outer table, Employees in this case. This table includes the columns
from the Timecards table, but for the employees there there is no timecard,
all columns have NULL. Which you apparently have understood, since you
the condition "lmpEmployeeID IS NULL". But then there is a lapse, and you
filter lmpTimecardDate despite it is not likely that there is a row in
Timecards where the date is non-NULL and the employee ID is NULL. (At least
one would hope so!) Moving the date condition to the ON clause addresses
the issue, as it now will be part of the condition that builds the
table that is then filtered by WHERE.

Personally, I would prefer to write this query with NOT EXISTS:

SELECT E.Employee
FROM Employees E
WHERE NOT EXISTS (SELECT *
FROM Timecards T
WHERE E.EmployeeID = T.lmpEmployeeID
AND T.lmpTimecardDate = '20071014')

Simply because this clearly express what this is all about.

And I would also use a date format that is safe from misinterpretations.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Thanks a lot guys...

That worked perfectly... and thanks for the explanation and
suggestions

Regards

Pablo

Oct 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: The Bit Bandit | last post by:
Hopefully someone can help me create a query that I'm having some trouble with. I have three tables: invoices, invoicedetails, invoicepayments The fields are: invoices -------- InvoiceNo
1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
2
by: commanderjason | last post by:
This seems like a very simple question but i have never been able to find an easy answer to it. I have a user table and i do a join with another table, we'll call the other table a results...
4
by: TGEAR | last post by:
Itemlookup table Field names : index_id (primary key), itemno, description. It has a child table, which is ItemPriceHistory table The relationship to the child table is one (parent table)-to-many...
2
by: tricard | last post by:
Good day all, I have a large outer joined query that I want to have some criteria. The select query is gathering all part numbers from tblPartNumbers, left joining to tblPartNumberVendor (since...
6
by: rshivaraman | last post by:
CREATE TABLE ( (10) NULL ) CREATE TABLE ( (10) NULL )
1
by: Vivienne | last post by:
Hi there This is a hard problem that I have - I have only been using sql for a couple of weeks and have gone past my ability level quickly! The real tables are complex but I will post a simple...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.