SQL Server 查询时间段
来源:互联网 发布:宽容房龙读后感知乎 编辑:程序博客网 时间:2024/04/29 07:31
create table ABSWorkLog
(
id int identity(1,1),
MachineCode varchar(50),
startTime smalldatetime,
endTime smalldatetime
)
go
insert absworklog values ('abs1','2008-02-02','2008-02-09')
insert absworklog values ('abs1','2008-02-13','2008-02-26')
insert absworklog values ('abs1','2008-03-02','2008-03-26')
insert absworklog values ('abs2','2008-01-26','2008-02-06')
insert absworklog values ('abs2','2008-02-24','2008-03-08')
insert absworklog values ('abs3','2008-02-07','2008-02-25')
go
select * from absworklog
select * into #abs1worklog from absworklog where machinecode='abs1'
select * from #abs1worklog
select * into #otherabsworklog from absworklog where machinecode<>'abs1'
select * from #otherabsworklog
select DISTINCT machinecode into #otherabsname from absworklog where machinecode<>'abs1'
select * from #otherabsname absworklog
create table #temp
(
machinecode varchar(50),
stopTime int
)
go
select * from #temp
declare @startTime smalldatetime,@endTime smalldatetime
DECLARE MyCursor CURSOR FOR
SELECT startTime,endTime FROM #abs1worklog
open MyCursor
FETCH NEXT FROM MyCursor INTO @startTime, @endTime
WHILE @@fetch_status = 0
BEGIN
select a.machinecode,
t=(case when b.startTime is null then convert(int,@endTime)-convert(int,@startTime)
when b.startTime<@startTime and b.endTime<@endTime then convert(int,@endTime)-convert(int,b.endTime)
when b.startTime>@startTime and b.endTime>@endTime then convert(int,b.startTime)-convert(int,@startTime)
when b.startTime>@startTime and b.endTime<@endTime then convert(int,b.startTime)-convert(int,@startTime)+convert(int,@endTime)-convert(int,b.endTime)
end)
into #mid from #otherabsname a
right join #otherabsworklog b on b.machinecode=a.machinecode
where (b.startTime<@startTime and b.endTime>@startTime and b.endTime<@endTime)
or (b.startTime>@startTime and b.endTime<@endTime)
or (b.startTime>@startTime and b.startTime<@endTime and b.endTime>@endTime)
or (b.starttime<@startTime and b.endTime>@endTime)
declare @name varchar(50),@t int
declare MidCurrsor CURSOR FOR
select machinecode,t from #mid
open MidCurrsor
FETCH NEXT FROM MidCurrsor INTO @name, @t
WHILE @@fetch_status = 0
BEGIN
insert #temp values (@name,@t)
FETCH NEXT FROM MidCurrsor INTO @name, @t
end
DEALLOCATE MidCurrsor
drop table #mid
FETCH NEXT FROM MyCursor INTO @startTime, @endTime
END
DEALLOCATE MyCursor
GO
select machinecode,sum(stoptime) from #temp GROUP BY machinecode
- SQL Server 查询时间段
- SQL Server 查询时间段问题
- 收藏 sql server 2000查询时四个时间段的交集!
- SQL Server 查询两个时间段是否有交集
- sql server 时间段查询(7:30-12:30)
- sql server 2012 查询时间段月份数据方法
- sql时间段查询
- SQL时间段查询
- SQL时间段查询
- SQL时间段查询
- SQL时间段查询
- sql时间段查询问题
- sql 时间段查询
- Sql 时间段查询
- SQL 时间段查询
- SQL时间段查询(转)
- mybatis查询时间段sql语句
- mybatis时间段查询(sql)
- 类型,转换,数组,协变及其他
- Asp.net读取Word的实例与总结
- SSAS: 再谈谈如何控制会话(Session)及其销毁
- .Net深入学习序列化和反序列化
- 优化JavaScript下载速度
- SQL Server 查询时间段
- 华为EC1260-通话短信 http://majszt.blog.hexun.com/33967361_d.html
- Transact-SQL不使用游标的两个循环方法_SQL技巧
- js控制文本框内回车事件--IE and FireFox
- MOSS : 如何动态修改列表视图定义
- 信息管理与信息系统
- phpwind 732之自定义首页
- bind9配置精讲(一)
- bind9配置精讲(二)