【发布时间】:2013-01-16 11:41:26
【问题描述】:
拥有:
SELECT DISTINCT TOP 100 * FROM mytable ORDER BY date ASC
如何使日期中的空值最后出现?
非常感谢。
【问题讨论】:
标签: sql tsql select distinct sql-order-by
拥有:
SELECT DISTINCT TOP 100 * FROM mytable ORDER BY date ASC
如何使日期中的空值最后出现?
非常感谢。
【问题讨论】:
标签: sql tsql select distinct sql-order-by
有些数据库支持 order by 中最后一个 NULL 的语法,有些则不支持。所以,我使用:
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
您也可以将 distinct 放在子查询中:
select top 100 *
from (select distinct *
from mytable
) t
order by (case when date is null then 1 else 0 end), date asc
假设date 在列列表中,但是,第一个版本应该可以工作。
【讨论】: