[英]SQL Select statements - more than one output column
假设我得到了这样的查询:
select count(*) as Morning from Table
where timestamp between '01.08.13 06:00:00' AND '01.08.13 10:00:00'
现在我想在中午和晚上有一个输出。 是的我可以做三个查询,但有可能创建一个返回所有三个(早上,中午,晚上)的选择语句吗?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.