gpt4 book ai didi

sql - 将空值排序在最后而不是最前面

转载 作者:行者123 更新时间:2023-12-02 04:47:05 25 4
gpt4 key购买 nike

我正在尝试实现一个排序程序,它将根据下拉列表的值进行排序。发生的情况是,当对列表进行排序时,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

最佳答案

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

关于sql - 将空值排序在最后而不是最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com