?
Solved

Having issues with an sql query Microsoft Access

Posted on 2008-10-29
7
Medium Priority
?
255 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
LVL 5

Accepted Solution

by:
jfmador earned 2000 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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

719 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