Sorting of 2 columns

  • Hi,

    I need a sorting a table by column1,column2 descending order.the my table rows are below.

    col1 col2

    0 0

    0 0

    2 3

    6 2

    6 4

    3 7

    -------

    My results should be

    6 4

    6 2

    3 7

    2 3

    ----------

    When i give select *from table order by col1,col2 desc .

    output diff which i am not expecting.

    Can you help me on this

  • You need to put the DESC keyword on both columns in your ORDER BY clause.

    i.e. select * from table order by col1 desc, col2 desc

  • I forgot to mention, in the ORDER BY clause, if you do not designate a sort order for a column, it will default to ascending (ASC).

  • Actually, to produce the results:

    6 4

    6 2

    3 7

    2 3

    You actually want to sort by:

    64

    62

    37

    23

    so:

    Select * from Table

    order by cast(col1 as varchar) + cast(col2 as varchar) Desc

    Signature is NULL

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply