Oracle
 sql >> Database >  >> RDS >> Oracle

Come ordinare una stessa colonna sia in ordine asc che in ordine desc

Puoi farlo con row_number() e partecipare autonomamente:

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;

MODIFICA:

Puoi farlo anche con rownum ma richiede un ulteriore select :

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;