时间(按日期)    状态为1的记录数  状态为0的记录数  白天的记录数  夜晚的记录数是查询结果的字段

解决方案 »

  1.   

    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 时间