按两列排序

时间:2018-02-19 04:49:06

标签: sql sql-server sql-server-2008 sql-server-2012 sql-server-2017

我有这样的表:

+----+------+-----------+-------------------+
| ID | Name | sortorder | overridesortorder |
+----+------+-----------+-------------------+
| 1  | kkkk | 4         | null              |
+----+------+-----------+-------------------+
| 2  | yyyy | 3         | null              |
+----+------+-----------+-------------------+
| 3  | zzzz | 2         | 4                 |
+----+------+-----------+-------------------+
| 4  | gggg | 1         | 3                 |
+----+------+-----------+-------------------+
| 5  | bbbb | null      | null              |
+----+------+-----------+-------------------+
| 6  | aaaa | null      | null              |
+----+------+-----------+-------------------+

我需要显示如下所示的顺序,使用以上两个排序顺序列对表进行排序(如果两个值都为null,则使用Name列排序)并从两列中获取最大值然后排序它:

+----+-------+-----------+-------------------+
| ID | Name  | sortorder | overridesortorder |
+----+-------+-----------+-------------------+
| 1  | kkkk  | **4**     | null              |
+----+-------+-----------+-------------------+
| 3  | zzzz  | 2         | **4**             |
+----+-------+-----------+-------------------+
| 2  | yyyy  | **3**     | null              |
+----+-------+-----------+-------------------+
| 4  | gggg  | 1         | **3**             |
+----+-------+-----------+-------------------+
| 6  | bbbb  | null      | null              |
+----+------+-----------+-------------------+
| 5  | aaaa  | null      | null              |
+----+------+-----------+-------------------+

4 个答案:

答案 0 :(得分:1)

好像你正在寻找这个

declare @t table(
    ID int
    , Name varchar(100)
    , sortorder int
    , overridesortorder int
)
insert into @t
values 
    (1, 'kkkk', 4, null)
    , (2, 'yyyy', 3, null)
    , (3, 'zzzz', 2, 4)
    , (4, 'gggg', 1, 3)
    , (5, 'bbbb', null, null)
    , (6, 'aaaa', null, null)

select
    *
from
    @t
order by coalesce(case when isnull(sortorder, 0) > isnull(overridesortorder, 0) then sortorder else overridesortorder end, -2147483648) desc, Id, Name

输出

ID  Name    sortorder  overridesortorder 
----------------------------------------
1   kkkk    4          NULL
3   zzzz    2          4
2   yyyy    3          NULL
4   gggg    1          3
5   bbbb    NULL       NULL
6   aaaa    NULL       NULL

答案 1 :(得分:0)

如果我正确理解您的方案,overridesortorder列只会覆盖sortorder列。您可以使用COALESCE返回第一个非空值,当两列都为null以返回零时,这是必不可少的。

ORDER BY COALESCE(overridesortorder, sortorder, 0) DESC, 
         ISNULL(sortorder, 0) DESC,
         Name

这是How to properly implement error handling in async/await case

答案 2 :(得分:0)

这是你想要的?

select * 
from table
order by coalesce(overridesortorder, sortorder) desc, overridesortorder

答案 3 :(得分:0)

试试这个,

SELECT  *
        ,(CASE WHEN ISNULL(sortorder,0) > ISNULL(overridesortorder,0)
          THEN ISNULL(sortorder,0) 
          ELSE ISNULL(overridesortorder,0) END) AS FinalSortOrder
FROM    YourTable
ORDER BY FinalSortOrder DESC
        ,sortorder DESC
        ,overridesortorder DESC
        ,Name DESC

Demo here