select 时间,[状态为1数]=sum(case when 状态=1 then 1 else 0 end), [状态为0数]=sum(case when 状态=0 then 1 else 0 end), [白天的记录数]=sum(case when DATEPART(hour, 时间) between 7 and 19 then 1 else 0 end), [夜晚的记录数]=sum(case when DATEPART(hour, 时间)>19 or DATEPART(hour, 时间)<7 then 1 else 0 end) from tbl group by 时间
[状态为0数]=sum(case when 状态=0 then 1 else 0 end),
[白天的记录数]=sum(case when DATEPART(hour, 时间)
between 7 and 19 then 1 else 0 end),
[夜晚的记录数]=sum(case when DATEPART(hour, 时间)>19 or
DATEPART(hour, 时间)<7 then 1 else 0 end)
from tbl
group by 时间