Help with SQL: value-dependent sorting?
I am trying to write a SQL query that does some complex sorting. I have a table with some records, each having three important fields:
- Group_ID
- Modified_Order
- Created_Order
Now, I am trying to write a query that first 开发者_JS百科sorts these by the group ID. Then if the group ID is 1, sort those results by their "modified order". If the group ID is 2, sort them by their "created order". Group ID's that are not 1 or 2 will not occur. In other words, I want the results to look like the picture below.
How can I write the "ORDER BY" or "GROUP BY" part of my query to do this?
Select RecordID, GroupID, Modified_Order, Created_Order
FROM TableName
ORDER BY GroupID, Case When GroupID = 1 Then Modified_Order Else Created_Order end
select * from jegad where gr_id in ('1','2') order by gr_id
How about:
select * from table order by group_id,group_id=1 and modified_order,group_id=2 and created_order;
精彩评论