分时统计各小区用户停车数

字段:用户id, 日期,开始停车时间,结束停车时间,小区id   ,进行分时间段统计各小区各用户的停车数

----------------------------------------------------------------------------------------------------------------------------------------------------------

user_id       |      date                  |        start_time            |      end_time           |  community_id       |        表名:user_parking_record
String          |      yyyy-MM-dd     |        hh:mm:ss            |       hh:mm:ss         |       String

----------------------------------------------------------------------------------------------------------------------------------------------------------

问题描述好像不够明确。以下查询为根据自定义时间段,统计历史停车数。如果是其他情况可根据这个语句进行调整

/*问题描述好像不够明确。
以下查询为根据自定义时间段,统计历史停车数。
时间字段类型是什么?字符串类型可直接截取,如果是时间类型还需要用to_char函数转换。
另外是什么数据库?不同数据库函数也会有个别差异。
如果是其他情况可根据这个语句进行调整*/
select date as 日期 ,community_id as 小区id,
count(case when substr(start_time,5) < '08:30') as 零点到八点半停车数,
count(case when substr(start_time,5) >= '08:30' and substr(start_time,2) < '12') as 八点半之后到十二点停车数,
count(case when substr(start_time,2) >= '12') as 十二点之后停车数
  from user_parking_record t 
 where date between 'yyyy-MM-dd' and 'yyyy-MM-dd' /*统计日期范围*/
 group by date,community_id;