April 6, 2011 at 5:37 am
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
April 6, 2011 at 5:46 am
Can you provide us with the query you are running?
April 6, 2011 at 6:01 am
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.
April 6, 2011 at 6:23 am
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.
April 6, 2011 at 6:27 am
Hi
The EmpId is identity column in the table
Thanks
Subrat
April 6, 2011 at 6:37 am
subrat_4ualways (4/6/2011)
HiThe 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
April 6, 2011 at 6:48 am
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.
April 6, 2011 at 7:04 am
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
April 6, 2011 at 7:19 am
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.
April 6, 2011 at 7:25 am
The order by is not the problem. You don't understand the way it works or the requirement is wrong.
April 6, 2011 at 8:11 am
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