Solved UNION QUERY.

smtazulislam

Member
Local time
Today, 12:25
Joined
Mar 27, 2020
Messages
808
This is my UNION QUERY.

Code:
SELECT qryVacationProgressList.EmployeeID, qryVacationProgressList.EmployeeName, qryVacationProgressList.CompanyName, qryVacationProgressList.MaxOfLeaveDate, qryVacationProgressList.BranchEn, qryVacationProgressList.Status
FROM qryVacationProgressList;
UNION SELECT qryFinalExitProgressList.EmployeeID, qryFinalExitProgressList.EmployeeName, qryFinalExitProgressList.CompanyName, qryFinalExitProgressList.MaxOfLeaveDate, qryFinalExitProgressList.BranchEn, qryFinalExitProgressList.ExitStatus
FROM qryFinalExitProgressList;

Result Display :
1661251691167.png

This result is perfect display. But problem is we don't know who is applied for Final Exit Or Vacation.

Can possible that, write, Progress - Vacation from vacation table And Progress - Final Exit from Final Exit table.
 
Try something like this:
Code:
SELECT qryVacationProgressList.EmployeeID, qryVacationProgressList.EmployeeName, qryVacationProgressList.CompanyName, qryVacationProgressList.MaxOfLeaveDate, qryVacationProgressList.BranchEn, qryVacationProgressList.Status, "Progress - Vacation" as Type
FROM qryVacationProgressList;
UNION SELECT qryFinalExitProgressList.EmployeeID, qryFinalExitProgressList.EmployeeName, qryFinalExitProgressList.CompanyName, qryFinalExitProgressList.MaxOfLeaveDate, qryFinalExitProgressList.BranchEn, qryFinalExitProgressList.ExitStatus, "Progress - Final exit" as Type
FROM qryFinalExitProgressList;
 
Try the following
Code:
SELECT qryVacationProgressList.EmployeeID, qryVacationProgressList.EmployeeName, qryVacationProgressList.CompanyName, qryVacationProgressList.MaxOfLeaveDate, qryVacationProgressList.BranchEn, qryVacationProgressList.Status,"Vacation" as MyStatus
FROM qryVacationProgressList
UNION SELECT qryFinalExitProgressList.EmployeeID, qryFinalExitProgressList.EmployeeName, qryFinalExitProgressList.CompanyName, qryFinalExitProgressList.MaxOfLeaveDate, qryFinalExitProgressList.BranchEn, qryFinalExitProgressList.ExitStatus, "FinalExit" as MyStatus
FROM qryFinalExitProgressList;
 
you can also use this:
Code:
SELECT qryVacationProgressList.EmployeeID, qryVacationProgressList.EmployeeName, qryVacationProgressList.CompanyName, qryVacationProgressList.MaxOfLeaveDate, qryVacationProgressList.BranchEn, qryVacationProgressList.Status + " - Vacation" As EmpStatus
FROM qryVacationProgressList
UNION
SELECT qryFinalExitProgressList.EmployeeID, qryFinalExitProgressList.EmployeeName, qryFinalExitProgressList.CompanyName, qryFinalExitProgressList.MaxOfLeaveDate, qryFinalExitProgressList.BranchEn, qryFinalExitProgressList.ExitStatus +  " - Final Exit" As EmpStatus FROM qryFinalExitProgressList;
 
And this is a good example on why it is important to normalize a database.
You could have avoided the union if you added a table for all the employees, a table for the companies, a table for the vacations and a table for the FinalExits.
All linked with ID's and you can select any employee with their vacation and finalExit using left joins.
 
All you need is for the fields count and types to match. You could extra fields to both parts, and populate some with blanks.

Instead of your query simply do

SELECT * FROM qryVacationProgressList
UNION SELECT * FROM qryFinalExitProgressList;


or if you don't need all the fields in the lists

SELECT * FROM q1
(where q1 is based on qryVacationProgressList)
UNION SELECT * FROM q2 (where q2 is based on qryFinalExitProgressList)


and then tweak both queries until you get the fields to match.
You can add sort by setting to this as well.
 

Users who are viewing this thread

Back
Top Bottom