Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Having issues with an sql query Microsoft Access

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
talker2004
Asked:
talker2004
  • 4
  • 2
1 Solution
 
RPCITCommented:
I would try this

ON (Employee.EmpKey = ApptData.ApptEmp) OR (ApptData.ApptEmp = 9999)
0
 
talker2004Author Commented:
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
 
talker2004Author Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jfmadorCommented:
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
 
RPCITCommented:
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
 
talker2004Author Commented:
Thanks everyone for you contribution, but i got to give points to the solution that worked.

0
 
talker2004Author Commented:
query worked the first time i ran it.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now