Sorting Multiple Columns

  • Hi

    I have a doubt in sorting of Multiple columns in Sql Server.Suppose I have a table where I want to sort the ID column in descending order & Salary column in ascending order.But after querying the output I am getting is on only the 1st Column.Its not taking into account the 2nd column for sorting.Please clarify the same.

    Thanks in advance

    Subrat

  • Can you provide us with the query you are running?

  • Do you have multiple rows per ID?

    Otherwise the Salary column in the sort is irrelevant.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Hi

    The query is

    Select * from emp_details order by EmpId desc,EmpSal

    The output I am getting is Empid desc only....

    Many Thanks

    Subrat.

  • Hi

    The EmpId is identity column in the table

    Thanks

    Subrat

  • subrat_4ualways (4/6/2011)


    Hi

    The query is

    Select * from emp_details order by EmpId desc,EmpSal

    The output I am getting is Empid desc only....

    Many Thanks

    Subrat.

    I think David nailed it, unless an EmpId appears more than once in the table, any other sorting cannot have an impact...

    since the EmpId is the identity, you'll not see anything.

    try sorting one something else...

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi

    Have a look at the below query..Am not using EmpId .The output am getting is only on the basis of EmpName.Not on both.

    The columns of emp_details are EmpId,EmpName,EmpSal,EmpGender

    Select * from emp_details order by EmpName asc,EmpSal desc

    Regards

    Subrat.

  • think about it: how many unique employees receive two salaries? i doubt any in this table.

    maybe you need to join to a different table, say the payroll table, so you can see a record for each week or pay period or something.

    try this. this will easily identify if an EmpName ever appears more than once in your emp_details table. If it returns no rows, then there's only one record per employee;

    SELECT * FROM

    (

    SELECT

    --does the EmpName ever appear more than ONCE in this table?

    ROW_NUMBER() OVER (PARTITION BY EmpName ORDER BY EmpName ASC,EmpSal DESC),

    emp_details.*

    FROM emp_details

    ) MyAlias

    WHERE RW = 2

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi

    Let me put my question this way.

    I have a table where in I want to show the results sorted by 2 columns

    the 1st column will sort data in descending order and the 2nd column will sort data in ascending order.

    Can I have the resultset satisfying both the sorted order.

    Thanks

    Subrat.

  • The order by is not the problem. You don't understand the way it works or the requirement is wrong.

  • Subrat,

    Hope your data as below

    EmpId | EmpSal

    ---------------

    120000

    210000

    315000

    What would be your expected result?

    _____________________________________________
    One ounce of practice is more important than tonnes of dreams

Viewing 11 posts - 1 through 10 (of 10 total)

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