假设我收到这样的查询:
select count(*) as Morning from Table
where timestamp between '01.08.13 06:00:00' AND '01.08.13 10:00:00'
现在我想有一个中午和晚上的输出。是的,我可以做三个查询,但是是否可以创建一个返回所有三个(早上、中午、晚上)的 select 语句?
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