How to sort a same column both in asc order and desc order

Avinash

With an SQL Query how do we get the output of 2 columns, the first one being a column sorted in asc order and the second one with the order desc and both are same columns.

ex:

emp table:
empid
1 
5
9
4

The query output should be

empid_1   empid_2
1          9
4          5
5          4
9          1

What OP tried so far

WITH emp1 
     AS (SELECT ROWNUM a, 
                empno 
         FROM   (SELECT empno 
                 FROM   emp 
                 ORDER  BY 1 ASC)), 
     emp2 
     AS (SELECT ROWNUM b, 
                empno 
         FROM   (SELECT empno 
                 FROM   emp 
                 ORDER  BY 1 DESC)) 
SELECT emp1.empno, 
       emp2.empno 
FROM   emp1, 
       emp2 
WHERE  emp1.a = emp2.b; 
Ben

If you use a common table expression/sub-query factoring clause then you only need to access the table once:

with the_data as (
 select empid
      , row_number() over ( order by empid ) as id_asc
      , row_number() over ( order by empid desc ) as id_desc
   from emp
        )
select a.empid as empid_asc
     , d.empid as empid_desc
  from the_data a
  join the_data d
    on a.id_asc = d.id_desc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sort a same column both in asc order and desc order

From Dev

Sort Asc and desc in the same column

From Dev

Order table column in asc/desc order

From Dev

Order By Asc and Desc both in single query

From Dev

Custom Comparator implementation for both ASC and DESC order

From Dev

Case statement for Order By clause with Desc/Asc sort

From Dev

SQL: Order by column, then by substring mix asc and desc

From Dev

Conditionally reverse ASC/DESC sort order in Access SQL with IF ELSE in ORDER BY?

From Dev

how to order by <= 7 desc then by > 7 asc?

From Dev

CASE Statement for Order By Clause with Multiple Columns and Desc/Asc Sort

From Dev

Mongoid Rails 4 sort by asc or desc order created_at

From Dev

PHP MySQL sort order ASC/DESC for only the records shown

From Dev

How do I sort data from a mysql db according to a unique and predetermined order, NOT asc or desc

From Dev

conditional asc or desc in in a order byexpression

From Dev

conditional asc or desc in in a order byexpression

From Dev

Order by in mysql without asc and desc

From Dev

MySQL ORDER BY multiple column ASC and DESC not working as expected

From Java

Sublime text 2 - how to order a list of words alphabetically (DESC / ASC)

From Dev

How to sort in desc order using primitive type?

From Dev

Should first_value() over (order by something asc) be the same as last_value () over (order by something desc)?

From Dev

Order by both ASC and DESC at a time in single CASE WHEN statement SQL Server

From Dev

Datastax Driver Mapping ORDER BY ASC/DESC

From Java

Room DAO Order By ASC or DESC variable

From Dev

PHP Mysql ASC AND DESC Order Proper use?

From Dev

We get data with ORDER BY ASC but NOT BY DESC

From Dev

PHP Mysql ASC AND DESC Order Proper use?

From Dev

Datastax Driver Mapping ORDER BY ASC/DESC

From Dev

sort hashtable by values in ASC order

From Dev

MySQL query faster in DESC order than ASC order

Related Related

  1. 1

    How to sort a same column both in asc order and desc order

  2. 2

    Sort Asc and desc in the same column

  3. 3

    Order table column in asc/desc order

  4. 4

    Order By Asc and Desc both in single query

  5. 5

    Custom Comparator implementation for both ASC and DESC order

  6. 6

    Case statement for Order By clause with Desc/Asc sort

  7. 7

    SQL: Order by column, then by substring mix asc and desc

  8. 8

    Conditionally reverse ASC/DESC sort order in Access SQL with IF ELSE in ORDER BY?

  9. 9

    how to order by <= 7 desc then by > 7 asc?

  10. 10

    CASE Statement for Order By Clause with Multiple Columns and Desc/Asc Sort

  11. 11

    Mongoid Rails 4 sort by asc or desc order created_at

  12. 12

    PHP MySQL sort order ASC/DESC for only the records shown

  13. 13

    How do I sort data from a mysql db according to a unique and predetermined order, NOT asc or desc

  14. 14

    conditional asc or desc in in a order byexpression

  15. 15

    conditional asc or desc in in a order byexpression

  16. 16

    Order by in mysql without asc and desc

  17. 17

    MySQL ORDER BY multiple column ASC and DESC not working as expected

  18. 18

    Sublime text 2 - how to order a list of words alphabetically (DESC / ASC)

  19. 19

    How to sort in desc order using primitive type?

  20. 20

    Should first_value() over (order by something asc) be the same as last_value () over (order by something desc)?

  21. 21

    Order by both ASC and DESC at a time in single CASE WHEN statement SQL Server

  22. 22

    Datastax Driver Mapping ORDER BY ASC/DESC

  23. 23

    Room DAO Order By ASC or DESC variable

  24. 24

    PHP Mysql ASC AND DESC Order Proper use?

  25. 25

    We get data with ORDER BY ASC but NOT BY DESC

  26. 26

    PHP Mysql ASC AND DESC Order Proper use?

  27. 27

    Datastax Driver Mapping ORDER BY ASC/DESC

  28. 28

    sort hashtable by values in ASC order

  29. 29

    MySQL query faster in DESC order than ASC order

HotTag

Archive