Reputation: 1151
I have 2 tables - Table A contains a list of employees that is effective dated ad the other table B contains employees reference details.
What I am trying to do is to sieve out a list of employees based on Emp_ID that are valid - Definition of valid is that Emp_status is A (Active) and for the particular date (current date) for that employee falls within the Effective_start and end date.
Table A has around 800 records and Table B has around 2000 records. How should I do this via SQL query such that it will be efficient?
I am thinking of filtering out the list of valid EMP_ID from table B (~500 records) and then join with table A based on EMP_ID and current date to see if its falls within the effective period. However, that would mean comparing every 500 records on the 2000 records of Table A, and Table A is expected to grow in future.
Any advice on tackling this issue?
Upvotes: 0
Views: 870
Reputation: 1171
You can join these two tables and omit out the columns not meeting the criteria in individual tables. Like for table A - Omit the ones for which the current date do not fit between the start and end dates and for table B - Omit the ones whose Emp_Status in not A (Active)
SELECT t1.Emp_ID , t1.Effective_Start, t1.Effective_End,
t2.Emp_Status
FROM table1 t1
JOIN table2 t2 ON t1.Emp_ID = t2.Emp_ID
WHERE (
CURDATE( ) BETWEEN t1.Effective_Start AND t1.Effective_End )
AND t2.Emp_Status = 'A'
);
Upvotes: 2