Solved

Having issues with an sql query Microsoft Access

Posted on 2008-10-29
7
247 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now