Solved

Having issues with an sql query Microsoft Access

Posted on 2008-10-29
7
251 Views
Last Modified: 2013-11-07
I am having issues with the following sql query in Microsoft Access.

SELECT ApptData.*, Procedures.ProcDescr
FROM Procedures
INNER JOIN ApptData ON Procedures.ProcKey = ApptData.ApptService
WHERE ApptData.ApptClient = 28 AND ApptData.ApptDate >= #10/29/2008#
ORDER BY ApptData.ApptDate ASC


Now i also want to join the employee table on Employee.EmpKey = ApptData.ApptEmp. I only need Employee.FirstName and Employee.LastName.

The only problem is that there is not always an employee record in the database associated with the appointment record. When this is the case the ApptData.ApptEmp will always be marked with employee number 9999. In the case where employee number is = to 9999 we can have an empty string or null value inside of the Employee.FirstName and Employee.LastName field.

Here is what i got so far but it does not bring back any records where ApptData.ApptEmp = 9999. This is because there is not corresponding record in the employee table.

 
SELECT ApptData.*, Procedures.ProcDescr, Employee.EmpFirstName,
Employee.EmpLastName
FROM Employee INNER JOIN
(Procedures INNER JOIN ApptData ON Procedures.ProcKey =
ApptData.ApptService)
ON Employee.EmpKey = ApptData.ApptEmp
WHERE ApptData.ApptClient = 28 AND ApptData.ApptDate >= #10/29/2008#
ORDER BY ApptData.ApptDate ASC


If someone can help me bring back dummy results where their is no matching join criteria for the employee table it would be greatly appreciated.
0
Comment
Question by:talker2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 22833262
I would try this

ON (Employee.EmpKey = ApptData.ApptEmp) OR (ApptData.ApptEmp = 9999)
0
 
LVL 7

Author Comment

by:talker2004
ID: 22833320
this solution brings back one appointment record for every employee i have in my database. It's not really what i am trying to do.
0
 
LVL 7

Author Comment

by:talker2004
ID: 22833337
I don't really want it to join if the employee number is = 9999. I need it to dummy data in the first / lastname fields when the employee number is 9999
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Accepted Solution

by:
jfmador earned 500 total points
ID: 22833426
Why you don't use a LEFT JOIN? if the employe number cannot be found in the Employee table it will return you null for the EmpFirstName and EmpLastName,  you can use Coalesce on these field to provide a default value  COALESCE(Employee.EmpFirstName, 'N/A') as EmpFirstName

SELECT ApptData.*, Procedures.ProcDescr, Employee.EmpFirstName,
Employee.EmpLastName
FROM (Procedures INNER JOIN ApptData ON Procedures.ProcKey =
ApptData.ApptService) LEFT JOIN Employee
ON Employee.EmpKey = ApptData.ApptEmp
WHERE ApptData.ApptClient = 28 AND ApptData.ApptDate >= #10/29/2008#
ORDER BY ApptData.ApptDate ASC
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 22833670
would this work to dummy up the data?  this uses a subselect and a union...
SELECT ApptData.*, Procedures.ProcDescr, Employee.EmpFirstName, 
Employee.EmpLastName
FROM (SELECT EmpKey, EmpLastName, EmpFirstName FROM Employee UNION SELECT 9999 AS EmpKey, 'DummyLast' AS EmpLastName, 'DummyFirst' AS EmpFirstName) Employee   INNER JOIN
(Procedures INNER JOIN ApptData ON Procedures.ProcKey = 
ApptData.ApptService)
ON Employee.EmpKey = ApptData.ApptEmp
WHERE ApptData.ApptClient = 28 AND ApptData.ApptDate >= #10/29/2008#
ORDER BY ApptData.ApptDate ASC

Open in new window

0
 
LVL 7

Author Comment

by:talker2004
ID: 22833810
Thanks everyone for you contribution, but i got to give points to the solution that worked.

0
 
LVL 7

Author Closing Comment

by:talker2004
ID: 31511274
query worked the first time i ran it.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question