Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Como classificar uma mesma coluna em ordem asc e desc


Você pode fazer isso com row_number() e auto-ingressar:
select e1.empid as empid_1, e2.empid as empid_2
from (select e.*, row_number() over (order by emp_id) as seqnum
      from emp e
     ) e1 join
     (select e.*, row_number() over (order by emp_id desc) as seqnum
      from emp e
     ) e2
     on e1.seqnum = e2.seqnum;

EDITAR:

Você também pode fazer isso com rownum mas requer um select adicional :
select e1.empid as empid_1, e2.empid as empid_2
from (select e.*, rownum as seqnum
      from (select e.* from emp e order by empid asc) e
     ) e1 join
     (select e.*, rownum as seqnum
      from (select e.* from emp e order by empid desc) e
     ) e2
     on e1.seqnum = e2.seqnum;