最后排序空值而不是第一个

时间:2011-06-29 18:30:48

标签: sql sql-server tsql sorting stored-procedures

我正在尝试实现一个排序过程,它将根据下拉列表的值进行排序。发生的情况是,在对列表进行排序时,NULL字段将被指定的替换值替换,在本例中为''。这是首先排序,因为我们正在按升序排序,现在我想要以最后的空值升序 BUT 。我怎么能实现这个呢?

ORDER BY l.IsActive DESC, 
        CASE WHEN @SortOrder = 1 THEN n.DisplayName
             WHEN @SortOrder = 2 THEN CASE ec.IsEquipmentRelated
                                            WHEN  1 THEN ISNULL(el.ShopID,'') + 
                                                    ISNULL(ec.EquipmentAbbr,'') + 
                                                    ISNULL(el.ClassSequenceNumber,'') + 
                                                    ISNULL(el.EquipmentComponent,'') + 
                                                    ISNULL(el.CompSequenceNumber,'')
                                            WHEN  0 THEN ISNULL(ec.EquipmentAbbr,'')
                                            ELSE NULL
                                      END
             ELSE l.DisplayName
        END

编辑:运行MS SQL Server 2008 / T-SQL

编辑: @Joe Stefanelli,我试过这个,它没有编译:

ORDER BY l.IsActive DESC, 
    CASE WHEN @SortOrder = 0 THEN l.DisplayName
         WHEN @SortOrder = 1 THEN CASE WHEN n.DisplayName = '' THEN 2 ELSE 1 END, n.DisplayName
         WHEN @SortOrder = 2 THEN 
                                  CASE ec.IsEquipmentRelated
                                        WHEN  1 THEN ISNULL(el.ShopID,'') + 
                                            ISNULL(ec.EquipmentAbbr,'') + 
                                            ISNULL(el.ClassSequenceNumber,'') + 
                                            ISNULL(el.EquipmentComponent,'') + 
                                            ISNULL(el.CompSequenceNumber,'')
                                        WHEN  0 THEN ISNULL(ec.EquipmentAbbr,'')
                                        ELSE NULL
                                  END
     END

4 个答案:

答案 0 :(得分:30)

...
ORDER BY CASE WHEN YourField = '' THEN 2 ELSE 1 END, YourField

答案 1 :(得分:3)

最好的方法是添加一个派生列,该列具有排序顺序值(CASE,用于检查其他列是否为空),然后按该列排序。

答案 2 :(得分:2)

您的第二个代码段将无法编译,因为您尝试在外部CASE的WHEN @SortOrder = 1分支中返回多个值。如果你对上述分支进行了一些微小的改动,你可以解决它:

  1. 分别将21更改为'2''1'

  2. 将逗号(,)替换为连接运算符(+)。

  3. 因此生成的ORDER BY子句如下所示:

    ORDER BY
      l.IsActive DESC, 
      CASE
        WHEN @SortOrder = 0 THEN l.DisplayName
        WHEN @SortOrder = 1 THEN
          CASE WHEN n.DisplayName = '' THEN '2' ELSE '1' END + n.DisplayName
        WHEN @SortOrder = 2 THEN 
          CASE ec.IsEquipmentRelated
            WHEN  1 THEN
              ISNULL(el.ShopID,'') + 
              ISNULL(ec.EquipmentAbbr,'') + 
              ISNULL(el.ClassSequenceNumber,'') + 
              ISNULL(el.EquipmentComponent,'') + 
              ISNULL(el.CompSequenceNumber,'')
            WHEN  0 THEN
              ISNULL(ec.EquipmentAbbr,'')
            ELSE NULL
          END
      END
    

答案 3 :(得分:1)

OK!我找到了一种方法来解决这个问题,结合所有三个答案。每个答案+1!我所做的是首先创建一个'虚拟'派生列,将空值分配给1,将非空值分配给0,先排序,然后排序其余:

ORDER BY 
   l.IsActive DESC, 
   CASE
    WHEN @SortOrder = 0 THEN (Case When l.DisplayName Is Null Then 1 Else 0 End)
    WHEN @SortOrder = 1 THEN (Case When n.DisplayName Is Null Then 1 Else 0 End)
    WHEN @SortOrder = 2 THEN (Case When ec.EquipmentAbbr Is Null Then 1 Else 0 End)
   END,
   CASE 
      WHEN @SortOrder = 0 THEN l.DisplayName
      WHEN @SortOrder = 1 THEN n.DisplayName
      WHEN @SortOrder = 2 THEN 
               ISNULL(el.ShopID,'') + 
               ISNULL(ec.EquipmentAbbr,'') + 
               ISNULL(el.ClassSequenceNumber,'') + 
               ISNULL(el.EquipmentComponent,'') + 
               ISNULL(el.CompSequenceNumber,'')
   END

这样,在实际排序完成之前,对字段是否为null进行排序,这会在结束时显示空值。