469,072 Members | 1,816 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,072 developers. It's quick & easy.

Can we use two orderBy in single Select statement ?

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
3 1381
ck9663
2,878 Expert 2GB
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
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
2,878 Expert 2GB
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.

Similar topics

5 posts views Thread by Joel | last post: by
6 posts views Thread by P. Emigh | last post: by
4 posts views Thread by Marc Gravell | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.