Sql Order By … using `Case When` for different Ascending, Descending, and Custom Orders

后端 未结 3 683
太阳男子
太阳男子 2021-02-08 00:04

I want to sort product by discount on certain condition

ORDER BY 
    CASE WHEN @OrderBy = 0
    THEN table.id END ASC,
    CASE WHEN @Orderby = 2
    THEN tabl         


        
3条回答
  •  花落未央
    2021-02-08 00:15

    It seems to me you need something similar to this

    select * from TableName where someCondition >100
      order by
        case when @OrderBy = 'AirlineService'
          then AirlineService END desc,
        case when  @OrderBy = 'SomeMore'
          then  MyOtherColumn  end
    GO
    

    If you not have a coulmn then you can not sort with that. Please read this Microsoft Link Please keep in mind - specifies the sort order used on columns returned in a SELECT statement. Hope it helps.

提交回复
热议问题