我建議你消除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