Order By in Sql Server

  • Hi All,

    In a table i want to apply order by. My database is 20-20 and table is errorreport where I have only 4 columns (rpt_id, error_id, file_id, errorlino). I have written the below query and it is working fine

    select * from errorreport

    where file_id=605 order by error_id desc

    the result is like below

    rpt_id error_id file_id errorlino

    46 95 605 1

    47 46 605 2

    48 41 605 3

    49 30 605 4

    But I don't want this. I want the list like below

    rpt_id error_id file_id errorlino

    46 95 605 1

    47 30 605 2

    48 41 605 3

    49 46 605 4

    I want to set the asc or desc order. Is it possible to do in SQL?

    Please help!

  • niladri.primalink (4/23/2013)


    Hi All,

    In a table i want to apply order by. My database is 20-20 and table is errorreport where I have only 4 columns (rpt_id, error_id, file_id, errorlino). I have written the below query and it is working fine

    select * from errorreport

    where file_id=605 order by error_id desc

    the result is like below

    rpt_id error_id file_id errorlino

    46 95 605 1

    47 46 605 2

    48 41 605 3

    49 30 605 4

    But I don't want this. I want the list like below

    rpt_id error_id file_id errorlino

    46 95 605 1

    47 30 605 2

    48 41 605 3

    49 46 605 4

    I want to set the asc or desc order. Is it possible to do in SQL?

    Please help!

    Not with ORDER BY, no - because you're not changing the order of the rows, you are changing the values in a column. If you are sure this is what you want to do, then you will have to explain the rules for moving the values around between rows.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • yes I want to change it..

    Please tell me the process.

    However thanks for your reply...

  • niladri.primalink (4/23/2013)


    yes I want to change it..

    Please tell me the process.

    However thanks for your reply...

    you will have to explain the rules for moving the values around between rows.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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