473,657 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ 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.Emplo yeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.Emplo yeeID =
Timecards.lmpEm ployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'

But it doesn't work. However, when I comment the date condition out
(lmpTimecardDat e = '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.Emplo yeeID
FROM Employees
WHERE Employees.Emplo yeeID not in (select Timecards.Emplo yeeID 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 19226
<pb*******@hotm ail.comwrote in message
news:11******** **************@ i13g2000prf.goo glegroups.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.Emplo yeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.Emplo yeeID =
Timecards.lmpEm ployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'

But it doesn't work. However, when I comment the date condition out
(lmpTimecardDat e = '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.Emplo yeeID
FROM Employees
WHERE Employees.Emplo yeeID not in (select Timecards.Emplo yeeID 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.Emplo yeeID
FROM Employees
LEFT OUTER JOIN Timecards
ON Employees.Emplo yeeID = Timecards.lmpEm ployeeID
AND lmpTimecardDate = '20071024'
WHERE lmpEmployeeID is NULL ;

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

But it doesn't work. However, when I comment the date condition out
(lmpTimecardDat e = '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 "lmpEmploye eID 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.lmpTimecardDa te = '20071014')

Simply because this clearly express what this is all about.

And I would also use a date format that is safe from misinterpretati ons.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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...@sommars kog.sewrote:
(pbassu...@hotm ail.com) writes:
SELECT Employess.Emplo yeeID
FROM Employees LEFT OUTER JOIN Timecards on Employees.Emplo yeeID =
Timecards.lmpEm ployeeID
WHERE lmpEmployeeID is NULL and lmpTimecardDate = '10/24/2007'
But it doesn't work. However, when I comment the date condition out
(lmpTimecardDat e = '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 "lmpEmploye eID 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.lmpTimecardDa te = '20071014')

Simply because this clearly express what this is all about.

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

--
Erland Sommarskog, SQL Server MVP, esq...@sommarsk og.se

Books Online for SQL Server 2005 athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.c om/sql/prodinfo/previousversion s/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
4853
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
4209
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" slows the system down considerably. I've tried creating a temp db, but I can't figure out how to execute two select commands. (It throws the exception "The column prefix 'tempdb' does not match with a table name or alias name used in the query.")
48
3850
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 query of all students in both tables with no duplicates. No clue whatsoever.
2
1417
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 table. The results table has numerous rows with the userid foreign key. I want to make a query that will give me the number of rows in the
4
1859
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 (child table). - It is possible to have no child record for some rows in the parent table. ItemPriceHistory table Field names: index_id (primary key), itemlookupID (foreign key of the Itemlookup table), date begin, price
2
3138
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 more than one vendor can make the part), then left joining to tblPartNumberSupplier (since more than one supplier can distribute the vendor's part), then left joining to tblPartNumberCost (since more than one cost can be associated with a single...
6
4018
by: rshivaraman | last post by:
CREATE TABLE ( (10) NULL ) CREATE TABLE ( (10) NULL )
1
2218
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 and a real version with the hope someone can help me. Any help would be much appreciated - I would also be happy to pay someone to actually do it if it takes time to work out as I know that its hard when all your help is free :)...
9
11915
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: http://www.codinghorror.com/blog/archives/000976.html
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7314
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.