I have one table as below which stores employees details
| empid | attribute | value | start_date | end_date |
|---|---|---|---|---|
| E1 | Active | FALSE | 2020-01-01 | 2022-05-05 |
| E1 | Active | TRUE | 2022-06-06 | 9999-12-31 |
| E1 | Branch | NYC | 2022-01-01 | 9999-12-31 |
| E2 | Active | TRUE | 2020-01-01 | 9999-12-31 |
Then another table which have address details
| empid | city |
|---|---|
| E1 | CON |
What I want is
- List of all the employee ids which do not have entries in address table
AND
- All those employees whose current value(today's value) of attribute ACTIVE is TRUE.
I have written below query but its taking too much time to execute (3-4mins). Is there any way i can optimize this query.
select distinct(emp.empid) from schema1.employees emp, schema2.address add
where emp.empid = add.empid
and (emp.attribute ='ACTIVE' and emp.val in ('TRUE')
and CURRENT_DATE BETWEEN emp.start_date and emp.end_date)
and emp.emp_id not in (select empid from schema2.address add2)
limit 20
select * from employees e left join address a on a.empid = e.empid where a.empid is null.DISTINCTreally necessary? I would guess there's going to be only one ACTIVE row for an employee in any specific date.distinctis not a function. It always applies to all columns in the select list. Enclosing one of the columns with parentheses won't change anything and is useless.distinct (a),bis the same asdistinct a,(b)ordistinct a,b