简体   繁体   中英

Oracle SQL - Checking status based on effective dates for list of employees

I have 2 tables - Table A contains a list of employees that is effective dated ad the other table B contains employees reference details.

  • Table A - 1) Emp_ID, 2)Effective_Start, 3)Effective_End
  • Table B - 1) Emp_ID, 2)Emp_status

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?

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' 
             ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM