(I correct the graph The employee_location table only has reference to the location)
I have the following tables that allow me to obtain the location of an employee
The Employee_information table may or may not have a reference value for the location code. So I should also look for it in the Location table. The problem is with the table employee_location . In this is the employee who is not referenced in employee_information , with one exception may appear multiple times in different cities. However, the report that I must submit ONLY must show one city per employee (at the request of the user).
So far I have this:
select coalesce(ei.name_value, l.name)
from employee e
INNER JOIN status s ON s.employee_id = e.employee_id
AND s.date between '2017-01-01' and '2017-10-01' -- Fechas a modo de ejemplo
Left join employee_information ei ON e.employee_id = ei.employee_id
Left join location l on l.location_id = ei.value
Left join (
select e1.employee_id, min(l1.location) as location
from employee e1
left join employee_location el1 on e.employee_id = el1.employee_id
left join location l1 on el1.location_id = l1.location_id
group by employee_id
) as temp_emp ON temp_emp.employee_id = e.employee_id
How can I improve this subquery ?. I do not want to look for the information of ALL the employees present in employee_location since it is quite heavy. It is possible to use the employees of the table with alias and instead of e1, since these have already been delimited with the dates in the status table. Or someone may recognize another, more optimal way of doing what I need.
So far I have not been able to find a reference that allows me to solve this concern.
This is only part of the final data that the report should contain.
Just to clarify. Another team of the company developed this model (and it is sometimes difficult to obtain clarifications from them), I only consult it.
I hope it was clear with the explanation of the problem and someone can give me a hand.
Thank you very much.