Solved

Having issues with an sql query Microsoft Access

Posted on 2008-10-29
7
249 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

776 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