I have two tables, one called Employees and the other called Assignments.
The Employees table consists of the following field:
SS
Name
EndDate
QuitReason
The Assignments table consists of:
Post
AssignedEmployeeSS
AssignmentEndDate
I first select the Post, AssignmentEndDate and AssignedEmployeeSS from the
Assignments table where the post is X and the assignment has ended, i.e.
AssignmentEndDate Is Not Null.
This query looks as follows:
SELECT Assignments.AssignmentEndDate, Assignments.AssignedEmployeeSS,
Assignments.Post
FROM Assignments
WHERE (((Assignments.AssignmentEndDate) Is Not Null) AND
((Assignments.Post)="X"));
I saved it and called it "AssignmentEndQuery."
From this data, I need to check and see if the employee actually quit the
company or just got transferred off the post. So if the employee's EndDate
is equal to the AssignmentEndDate, then he/she quit the post when they quit
the company and I need to show his/her Name as well as the QuitReason. If
the EndDate is not equal to the AssignmentEndDate, then the employee did not
quit and I just need to show the Name.
The following successfully retrieves the data I need:
SELECT AssignmentEndQuery.SS, AssignmentEndQuery.AssignmentEndDate,
Employees.QuitReason, Employees_1.[Employee Name]
FROM (Employees RIGHT JOIN AssignmentEndQuery ON (Employees.EndDate =
AssignmentEndQuery.AssignmentEndDate) AND (Employees.SS =
AssignmentEndQuery.SS)) INNER JOIN Employees AS Employees_1 ON
AssignmentEndQuery.SS = Employees_1.SS;
However, I can't use it in that form. I need one query, not two separate
ones.
Your help is much appreciated.
Maybe consider a different database design. Note that the CHECK constraints
would be better as validation rules in table design. This is merely a
suggestion. It takes time to develop a good database design, but without it you
are begging for problems.
Sub CreateTables()
With CurrentProject.Connection
..Execute _
"CREATE TABLE Employees" & _
" (EmployeeSS CHAR (9) NOT NULL PRIMARY KEY" & _
", EmployeeName VARCHAR (50) NOT NULL);"
..Execute _
"CREATE TABLE EmploymentHistory" & _
" (EmployeeSS CHAR (9) NOT NULL" & _
" REFERENCES Employees(EmployeeSS)" & _
", HireDate DATETIME NOT NULL" & _
", TerminateDate DATETIME NULL" & _
", CONSTRAINT ck_terminate_after_hire_date" & _
" CHECK (TerminateDate >= HireDate)" & _
", TerminateReason VARCHAR (20) NULL" & _
", CONSTRAINT ck_valid_terminate_reason" & _
" CHECK (TerminateReason IN ('Quit', 'Fired', 'Died'))" & _
", PRIMARY KEY (EmployeeSS, HireDate));"
..Execute _
"CREATE TABLE PostNames" & _
"(PostName VARCHAR (20) NOT NULL PRIMARY KEY);"
..Execute _
"CREATE TABLE EmployeePostings" & _
"(EmployeeSS CHAR (9) NOT NULL" & _
", HireDate DATETIME NOT NULL" & _
", CONSTRAINT fk_employment_history" & _
" FOREIGN KEY (EmployeeSS, HireDate)" & _
" REFERENCES EmploymentHistory (EmployeeSS, HireDate)" & _
", PostName VARCHAR (30) NOT NULL" & _
" REFERENCES PostNames (PostName)" & _
", AssignDate DATETIME NOT NULL" & _
", CONSTRAINT ck_assign_date_not_before_hire_date" & _
" CHECK (AssignDate >= HireDate)" & _
", DropDate DATETIME NULL" & _
", CONSTRAINT ck_drop_date_after_last_assign_date" & _
" CHECK (DropDate >= AssignDate)" & _
", DropReason VARCHAR (20) NULL" & _
", CONSTRAINT ck_valid_drop_reason" & _
" CHECK (DropReason IN ('Reassigned','Quit','Fired','Died'))" & _
", PRIMARY KEY (EmployeeSS, HireDate, PostName,AssignDate));"
End With
End Sub