Hey guys,
I have been mulling over this problem for a few days and have yet to come up with a query that will give me the expected results. I am working on a database that stores employee information, and my boss has asked me to add a component that will keep track of job vacancies as well.
I have 6 tables that are all linked:
Table: Employee
1. ID; Text; PK
2. ... (the rest are irrelevant to the query)
Table: EmplStatus (linked to Employee.ID)
1. EmployeeID; Text; PK
2. Date; Date/Time; PK
3. StatusCode; Text
4. Active; Yes/No
Table: EmplOrganizatio n (linked to Employee.ID)
1. EmployeeID; Text; PK
2. Date; Date/Time; PK
3. DepartmentCode; Text
4. DivisionCode; Text
5. Active; Yes/No
Table: EmplJob (linked to Employee.ID and Job.Code)
1. EmployeeID; Text; PK
2. Date; Date/Time; PK
3. JobCode; Text
4. Active; Yes/No
Table: Job
1. Code; Text; PK
2. Title; Text
3. ...(the rest are irrelevant to query)
Table: JobVacancy (linked to Job.Code)
1. JobCode; Text; PK
2. DepartmenCode; Text; PK
3. DivisionCode; Text; PK
4. NumberofPositio ns; Number
Now, what I'm trying to do is create a query that will show me all of the jobs in the job table, JobVacancy.Numb erofPositions for each job, all employees associated with that particular job title in a particular department/division. For example, I may have 20 Area Engineers, where 5 are in Department: A, Division: X, 10 are in Department: B, Division: Y, and 5 are in Department: A, Division: Z. Some critera is: EmplStatus.Acti ve=True, EmplJob.Active= True, EmplOrganizatio n.Active=True, EmplStatus.Stat usCode='A'.
The end result that I'm looking for is a list of all employees and their job code, department, and division. The closest result that I have gotten leaves out 5 of the employees and I can't figure out why. The reason for needing the employee ID's in my query is so that I can do a count of how many employees are in each department/division with a certain job title, so that I can subtract that from the Number of Positions, in order to determine how many vacancies there are. Here is my current SQL code:
[CODE=sql]SELECT DISTINCT Employee.ID, EmplJob.Employe eID, EmplJob.JobCode , EmplOrganizatio n.DepartmentCod e, EmplOrganizatio n.DivisionCode, EmplStatus.Stat usCode, Department.Desc , Division.Desc, JobVacancy.Numb erofPositions, JobVacancy.Depa rtmenCode
FROM (Job RIGHT JOIN (Department RIGHT JOIN (((Employee LEFT JOIN EmplJob ON Employee.ID = EmplJob.Employe eID) LEFT JOIN (Division RIGHT JOIN EmplOrganizatio n ON Division.Code = EmplOrganizatio n.DivisionCode) ON Employee.ID = EmplOrganizatio n.EmployeeID) LEFT JOIN EmplStatus ON Employee.ID = EmplStatus.Empl oyeeID) ON Department.Code = EmplOrganizatio n.DepartmentCod e) ON Job.Code = EmplJob.JobCode ) LEFT JOIN JobVacancy ON Job.Code = JobVacancy.JobC ode
WHERE (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode)=[EmplOrganizatio n].[DepartmentCode]) AND ((JobVacancy.Di visionCode)=[EmplOrganizatio n].[DivisionCode]) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Not Null) AND ((JobVacancy.Di visionCode) Is Not Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Not Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode)=[EmplOrganizatio n].[DivisionCode]) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode)=[EmplOrganizatio n].[DepartmentCode]) AND ((JobVacancy.Di visionCode) Is Not Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Not Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Not Null) AND ((JobVacancy.Di visionCode)=[EmplOrganizatio n].[DivisionCode]) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Not Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True))
ORDER BY EmplJob.JobCode , EmplOrganizatio n.DepartmentCod e, EmplOrganizatio n.DivisionCode;[/CODE]
If anyone can help me to try get the query to display the expected results I'd appreciate it. The problem I'm having is making sure that the employee ID only shows up once in the query result - I need the employee to only show up with the job code, department and division that they are associated with. I am having troubles where some job codes might be associated with more than one department and division - in those cases, I have had results where an employee ID will show up the same number of occurrences as there are different department/divisions associated with the job code. I hope someone can make sense of this and try to help me out! Thanks!
I have been mulling over this problem for a few days and have yet to come up with a query that will give me the expected results. I am working on a database that stores employee information, and my boss has asked me to add a component that will keep track of job vacancies as well.
I have 6 tables that are all linked:
Table: Employee
1. ID; Text; PK
2. ... (the rest are irrelevant to the query)
Table: EmplStatus (linked to Employee.ID)
1. EmployeeID; Text; PK
2. Date; Date/Time; PK
3. StatusCode; Text
4. Active; Yes/No
Table: EmplOrganizatio n (linked to Employee.ID)
1. EmployeeID; Text; PK
2. Date; Date/Time; PK
3. DepartmentCode; Text
4. DivisionCode; Text
5. Active; Yes/No
Table: EmplJob (linked to Employee.ID and Job.Code)
1. EmployeeID; Text; PK
2. Date; Date/Time; PK
3. JobCode; Text
4. Active; Yes/No
Table: Job
1. Code; Text; PK
2. Title; Text
3. ...(the rest are irrelevant to query)
Table: JobVacancy (linked to Job.Code)
1. JobCode; Text; PK
2. DepartmenCode; Text; PK
3. DivisionCode; Text; PK
4. NumberofPositio ns; Number
Now, what I'm trying to do is create a query that will show me all of the jobs in the job table, JobVacancy.Numb erofPositions for each job, all employees associated with that particular job title in a particular department/division. For example, I may have 20 Area Engineers, where 5 are in Department: A, Division: X, 10 are in Department: B, Division: Y, and 5 are in Department: A, Division: Z. Some critera is: EmplStatus.Acti ve=True, EmplJob.Active= True, EmplOrganizatio n.Active=True, EmplStatus.Stat usCode='A'.
The end result that I'm looking for is a list of all employees and their job code, department, and division. The closest result that I have gotten leaves out 5 of the employees and I can't figure out why. The reason for needing the employee ID's in my query is so that I can do a count of how many employees are in each department/division with a certain job title, so that I can subtract that from the Number of Positions, in order to determine how many vacancies there are. Here is my current SQL code:
[CODE=sql]SELECT DISTINCT Employee.ID, EmplJob.Employe eID, EmplJob.JobCode , EmplOrganizatio n.DepartmentCod e, EmplOrganizatio n.DivisionCode, EmplStatus.Stat usCode, Department.Desc , Division.Desc, JobVacancy.Numb erofPositions, JobVacancy.Depa rtmenCode
FROM (Job RIGHT JOIN (Department RIGHT JOIN (((Employee LEFT JOIN EmplJob ON Employee.ID = EmplJob.Employe eID) LEFT JOIN (Division RIGHT JOIN EmplOrganizatio n ON Division.Code = EmplOrganizatio n.DivisionCode) ON Employee.ID = EmplOrganizatio n.EmployeeID) LEFT JOIN EmplStatus ON Employee.ID = EmplStatus.Empl oyeeID) ON Department.Code = EmplOrganizatio n.DepartmentCod e) ON Job.Code = EmplJob.JobCode ) LEFT JOIN JobVacancy ON Job.Code = JobVacancy.JobC ode
WHERE (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode)=[EmplOrganizatio n].[DepartmentCode]) AND ((JobVacancy.Di visionCode)=[EmplOrganizatio n].[DivisionCode]) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Not Null) AND ((JobVacancy.Di visionCode) Is Not Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Not Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((Employee.ID) =[EmplJob].[EmployeeID]) AND ((EmplJob.JobCo de) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode)=[EmplOrganizatio n].[DivisionCode]) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode)=[EmplOrganizatio n].[DepartmentCode]) AND ((JobVacancy.Di visionCode) Is Not Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Not Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Null) AND ((JobVacancy.Di visionCode) Is Null) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplStatus.St atusCode)='A') AND ((JobVacancy.De partmenCode) Is Not Null) AND ((JobVacancy.Di visionCode)=[EmplOrganizatio n].[DivisionCode]) AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Not Null) AND ((EmplOrganizat ion.DivisionCod e) Is Null) AND ((EmplStatus.St atusCode)='A') AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True)) OR (((EmplJob.JobC ode) Is Not Null) AND ((EmplOrganizat ion.DepartmentC ode) Is Null) AND ((EmplOrganizat ion.DivisionCod e) Is Not Null) AND ((EmplStatus.St atusCode)='A') AND (([EmplStatus.Acti ve])=True) AND (([EmplOrganizatio n.Active])=True) AND (([EmplJob.Active])=True))
ORDER BY EmplJob.JobCode , EmplOrganizatio n.DepartmentCod e, EmplOrganizatio n.DivisionCode;[/CODE]
If anyone can help me to try get the query to display the expected results I'd appreciate it. The problem I'm having is making sure that the employee ID only shows up once in the query result - I need the employee to only show up with the job code, department and division that they are associated with. I am having troubles where some job codes might be associated with more than one department and division - in those cases, I have had results where an employee ID will show up the same number of occurrences as there are different department/divisions associated with the job code. I hope someone can make sense of this and try to help me out! Thanks!
Comment