Can we use two orderBy in single Select statement ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hellboss
    New Member
    • Jun 2007
    • 50

    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,
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by hellboss
    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:

    Code:
    select * from 
    (SELECT     PosID,PosID as PosID2
    FROM         tableXX
    WHERE     (condition = 'XX')) YourTableXX
    ORDER BY PosID ASC,PosID2 DESC
    -- CK

    Comment

    • hellboss
      New Member
      • Jun 2007
      • 50

      #3
      Originally posted by ck9663
      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:

      Code:
      select * from 
      (SELECT     PosID,PosID as PosID2
      FROM         tableXX
      WHERE     (condition = 'XX')) YourTableXX
      ORDER BY PosID ASC,PosID2 DESC
      -- 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 ?

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        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

        Comment

        Working...