By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,378 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,378 IT Pros & Developers. It's quick & easy.

Can we use two orderBy in single Select statement ?

P: 50
Hi every one !

Im workin on SQL05, My Question is !

i have a single field "PosId" in the DB,which i use as two fields one as PosId and PosID2 , Even though Both PosID & PosID2 are from the same fields ,i want to sort the PosID in ascending and PosID2 in desc,
This is what i tried , But ???

"SELECT PosID,PosID as PosID2
FROM tableXX
WHERE (condition = 'XX')
ORDER BY PosID ASC,PosID2 DESC"

Can we use two order by in a single select statement ?
Or is there any alternative .

Plz provide me with a valid soln.
Thanks in advance,
Apr 18 '08 #1
Share this Question
Share on Google+
3 Replies


ck9663
Expert 2.5K+
P: 2,878
Hi every one !

Im workin on SQL05, My Question is !

i have a single field "PosId" in the DB,which i use as two fields one as PosId and PosID2 , Even though Both PosID & PosID2 are from the same fields ,i want to sort the PosID in ascending and PosID2 in desc,
This is what i tried , But ???

"SELECT PosID,PosID as PosID2
FROM tableXX
WHERE (condition = 'XX')
ORDER BY PosID ASC,PosID2 DESC"

Can we use two order by in a single select statement ?
Or is there any alternative .

Plz provide me with a valid soln.
Thanks in advance,
I don't know what you're trying to accomplished. But if that's really your requirement, the answer is NO. You can not do it in a single select statement. You can, however, do it using subquery

Something like:

Expand|Select|Wrap|Line Numbers
  1. select * from 
  2. (SELECT     PosID,PosID as PosID2
  3. FROM         tableXX
  4. WHERE     (condition = 'XX')) YourTableXX
  5. ORDER BY PosID ASC,PosID2 DESC
  6.  
-- CK
Apr 18 '08 #2

P: 50
I don't know what you're trying to accomplished. But if that's really your requirement, the answer is NO. You can not do it in a single select statement. You can, however, do it using subquery

Something like:

Expand|Select|Wrap|Line Numbers
  1. select * from 
  2. (SELECT     PosID,PosID as PosID2
  3. FROM         tableXX
  4. WHERE     (condition = 'XX')) YourTableXX
  5. ORDER BY PosID ASC,PosID2 DESC
  6.  
-- CK
Thanks for your response, Well ! it worked partialy in displaying the same field in the data set but, one should be in ascending and other should be in descending order !

The above code didnt sort the PosID in desc order ?
Jun 3 '08 #3

ck9663
Expert 2.5K+
P: 2,878
Could you post some sample data?

If you are sorting the same field in different order, it will not work. Since your POSID will always be equal to POSID2 (since POSID2 is an alias), it will always be sorted by POSID.

-- CK
Jun 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.