假设我得到了这样的查询:
select count(*) as Morning from Table
where timestamp between '01.08.13 06:00:00' AND '01.08.13 10:00:00'
现在我想在中午和晚上有一个输出。是的我可以做三个查询,但有可能创建一个返回所有三个(早上,中午,晚上)的选择语句吗?
答案 0 :(得分:4)
select sum(case when timestamp between '01.08.13 06:00:00'
and '01.08.13 10:00:00'
then 1
else 0
end) as Morning,
sum(case when timestamp between '01.08.13 10:00:00'
and '01.08.13 14:00:00'
then 1
else 0
end) as Noon,
sum(case when timestamp between '01.08.13 14:00:00'
and '01.08.13 18:00:00'
then 1
else 0
end) as Evening
from Table