The question is a little asked.
order the result in asc or desc depending on the value of the column.
A column takes many values (since there are several rows).
Now the order by clause uses the expression and orders the lines on it. This expression must be morphotropic (;))
So, assuming a stardard oracle employee schema, managers:
select * from emp e where exists (select emp_id from emp where e.id=emp.mgr_id)
A workaround may be:
Select e.id, e.name, e.birth_date, case when (select count(*) from emp e where exists (select emp_id from emp where e.id=emp.mgr_id) ) --existence of manager > 0 then birth_date - to_date('1-Jan-1000','dd-mon-yyyy') else to_date('1-Jan-1000','dd-mon-yyyy') - birth_date end as tricky_expression from emp A order by 4;
This exexpresion is a case ; Using a constant (a subquery that decides that there are managers), it changes the values from positive to negative, i.e. Changes the direction of the order.
UPDATE: with information in the comments:
select id, name, birth_date emp_type from ( Select id, name, birth_date, emp_type, case when cnt_mgr > 0 then birth_date - to_date('1-Jan-1000','dd-mon-yyyy') else to_date('1-Jan-1000','dd-mon-yyyy') - birth_date end as tricky_expression from( Select e.id, e.name, e.birth_date, emp_type, count(case when emp_type='M' then 1 else 0 end) over() as mgr_count from emp A where your_conditions ) order by tricky_expression ) where rownum=1;
Florin ghita
source share