繁体   English   中英

TSQL SELECT DISTINCT,按ASC NULLS顺序排序

[英]TSQL SELECT DISTINCT with order by ASC NULLS last

具有:

SELECT DISTINCT TOP 100 * FROM mytable ORDER BY date ASC

我该如何使日期中的空值持续到最后?

非常感谢。

一些数据库支持NULL的语法,其后order by ,有些则不。 因此,我使用:

select distinct top 100 *
from MyTable
order by  (case when date is null then 1 else 0 end), date asc

或者,如果我不想输入太多内容:

order by coalesce(date, '9999-12-12')  -- or something like that

您还可以将不重复项放在子查询中:

select top 100 *
from (select distinct *
      from mytable
     ) t
order by (case when date is null then 1 else 0 end), date asc

但是,假设该date在列列表中,则第一个版本应该可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM