Reputation: 140
I have an employee table as below
I need to select employee_id, manager_id, result. result should have true or false depends on below conditions.
I came up with a query, but I need to know if there are any other better way to do it. This is my query
with manager as
(
select distinct manager_id from employee where manager_id is not null
)
select
e.employee_id, e.manager_id , m.manager_id,
case when e.manager_id is not null then true
when m.manager_id is not null then true
else false
end as Result
from employee e left join manager m on e.employee_id = m.manager_id
and the result should come like this
Upvotes: 2
Views: 68
Reputation: 43646
Maybe something like this:
select
e.employee_id,
e.manager_id,
(
e.manager_id is not null
or
exists(select 1 from employee m where m.manager_id=e.employee_id)
) as result
from employee e;
Upvotes: 2