Order by results by different columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javakid
    New Member
    • Mar 2007
    • 23

    Order by results by different columns

    Hi all
    I have results with different columns and i want to order the data according to different columns under different conditions.

    I have four columns: isflagged(T/F), FlaggedAt(Time) , createdAt(time) ,updatedAt(time ).

    For the rows which have isFlagged as T, i want them at top in result, order by FlaggedAt descending.
    and for the rows which have isFlagged as F , want them to be order by createdAt when created is not null otherwise by updatedAt.


    How can I acheieve this,

    Thanks
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    #2
    have you tried splitting the select into two, ordering them both seperately, and then unioning (if thats a word) them together....

    Code:
    select * from tbl where isFlagged = 't' Order By X
    UNION
    select * from tbl where isFlagged = 'f' Order By Y
    UNION
    select * from tbl where isFlagged = null Order By Z

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by harshmaul
      have you tried splitting the select into two, ordering them both seperately, and then unioning (if thats a word) them together....

      Code:
      select * from tbl where isFlagged = 't' Order By X
      UNION
      select * from tbl where isFlagged = 'f' Order By Y
      UNION
      select * from tbl where isFlagged = null Order By Z
      You cannot ORDER within a UNION select clause.

      Ronald

      Comment

      Working...