我建议你消除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
这仍然返回NULL – 2013-05-14 19:38:42
是你的'targetcompletedate'有时'null'?我更新了我的答案。 – 2013-05-14 20:46:54