借助master..spt_values表

按照时间(半小时)划分统计时间段:

select
cast(DATEADD(MINUTE,B.number*30,dateInfo.dday) as time) StartTime,
cast(DATEADD(SECOND,-1,DATEADD(MINUTE,(B.number+1)*30,dateInfo.dday)) as time) EndTime
from
(select top 1 convert(varchar(10),'07:00',120) dday) dateInfo
cross join master..spt_values b
where b.type = 'P' and number <24

统计时间段(半小时)范围内总数量:

with cte as
(
select
cast(DATEADD(MINUTE,B.number*30,dateInfo.dday) as time) StartTime,
cast(DATEADD(SECOND,-1,DATEADD(MINUTE,(B.number+1)*30,dateInfo.dday)) as time) EndTime
from
(select top 1 convert(varchar(10),'07:00',120) dday) dateInfo
cross join master..spt_values b
where b.type = 'P' and number <24
)
select
StartTime,
EndTime,
sum(PreSum) PreSum,
sum(PreUsed) PreUsed
from
(
select StartTime,EndTime,case when hbStartTime is null then 0 else 1 end PreSum,case when Used ='' then 0 else 1 end PreUsed
from
cte dateInfo
left join yshb_timetable b on cast(b.hbStartTime as time) between StartTime and EndTime
where b.YshbID='查询条件'
)a
group by StartTime,EndTime

最新文章

  1. CentOS 6.5 Python Image Library 配置
  2. VS中使用svn注意事项
  3. Eclipse中安装配置Tomcat
  4. linux后台运行和关闭、查看后台任务
  5. IEqualityComparer&lt;T&gt;
  6. Django 大文件下载
  7. Hibernate的单向OneToMany、单向ManyToOne
  8. ssl https服务 需要 php5.3以上
  9. [LeetCode] 179. Largest Number 解题思路
  10. Java中WebService实例
  11. MVC5搜索/查询 流程功能的实现
  12. mysql数据一致性检查及修复
  13. iOS 沙盒
  14. MySQL EXPLAIN 命令: 查看查询执行计划
  15. [LeetCode] Rank Scores -- 数据库知识(mysql)
  16. 函数的name属性
  17. edis 以及 Python操作Redis
  18. c++复习:STL之理论基础
  19. java ATM模拟程序
  20. C#学习笔记(五):while循环和for循环

热门文章

  1. 爬虫request库规则与实例
  2. dvaJS Model之间的调用
  3. 记一次SQL PLUS 不能登录的异常处理
  4. 20.Vue中获取DOM元素和组件
  5. Alpha版本发布
  6. web开发常见的鉴权方式
  7. 第五次个人作业---Alpha2项目测试
  8. G1垃圾收集器深度理论讲解【纯理论】
  9. UML再论关系extend和include
  10. python多线程实现ping多个ip