我建议你datediff()
完全消除:
Select (CASE when targetcompletedate <= NOW() the 'Overdue' else 'Days Left' end)
如果您想将事物显示为数字,那么您需要datediff()
. 为清楚起见,我将显式转换为字符串:
select (case when targetcompletedate <= NOW() then 'Overdue'
else cast(DATEDIFF(targetcompletedate, NOW()) as varchar(255))
end)
也许:
select (case when targetcompletedate <= NOW() then 'Overdue'
else concat(DATEDIFF(targetcompletedate, NOW()), ' days left')
end)
哲学是:如果有一种更简单、更清晰的方式来表达你想要的东西,就不要使用函数。
但是,我想知道您是否要计算每组中的数字:
select sum(case when targetcompletedate <= NOW() then 1 else 0 end) as NumOverdue,
sum(case when targetcompletedate <= NOW() then 0 else 1 end) as NumWithDaysLeft