首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何取特定时间段内系统中正在处理的数据

2012-04-18 
怎么取特定时间段内系统中正在处理的数据一个生产管理系统,有好几道工序,每件产品在不同工序中的id是不变

怎么取特定时间段内系统中正在处理的数据
一个生产管理系统,有好几道工序,每件产品在不同工序中的id是不变的,现在想查询在某个特定的时间段内,在系统中各个工序正在处理的所有的产品信息,求思路

[解决办法]
请提供表结构、测试数据、算法和你想要的结果
[解决办法]

SQL code
if object_id('[firstbegin]') is not null drop table [firstbegin]gocreate table [firstbegin]([id] int,[firststarttime] datetime)insert [firstbegin]select 1,'2011-11-22 10:45:01.000' union allselect 2,'2011-12-01 10:45:01.000' union allselect 3,'2011-12-01 10:45:01.000'if object_id('[firstend]') is not null drop table [firstend]gocreate table [firstend]([id] int,[firstendtime] datetime)insert [firstend]select 1,'2011-11-30 10:45:01.000' union allselect 2,'2011-12-08 10:45:01.000' union allselect 3,'2011-12-09 10:45:01.000'if object_id('[secondbegin]') is not null drop table [secondbegin]gocreate table [secondbegin]([id] int,[secondstarttime] datetime)insert [secondbegin]select 1,'2011-11-19 11:45:01.000' union allselect 2,'2011-12-09 11:45:01.000' union allselect 3,nullif object_id('[secondend]') is not null drop table [secondend]gocreate table [secondend]([id] int,[secondendtime] datetime)insert [secondend]select 1,'2011-11-30 11:45:01.000' union allselect 2,null union allselect 3,nullselect a.id,a.firststarttime,b.firstendtime,c.secondstarttime,d.secondendtimefrom firstbegin aleft join firstend b on a.id=b.idleft join secondbegin c on a.id=c.idleft join secondend d on a.id=d.idwhere d.secondendtime is null/**id          firststarttime          firstendtime            secondstarttime         secondendtime----------- ----------------------- ----------------------- ----------------------- -----------------------2           2011-12-01 10:45:01.000 2011-12-08 10:45:01.000 2011-12-09 11:45:01.000 NULL3           2011-12-01 10:45:01.000 2011-12-09 10:45:01.000 NULL                    NULL(2 行受影响)**/
[解决办法]
SQL code
--> 测试数据: @firstbegindeclare @firstbegin table (id int,firststarttime datetime)insert into @firstbeginselect 1,'2011-11-22 10:45:01.000' union allselect 2,'2011-12-01 10:45:01.000' union allselect 3,'2011-12-01 10:45:01.000'--> 测试数据: @firstenddeclare @firstend table (id int,firstendtime datetime)insert into @firstendselect 1,'2011-11-30 10:45:01.000' union allselect 2,'2011-12-08 10:45:01.000' union allselect 3,'2011-12-09 10:45:01.000'--> 测试数据: @secondbegindeclare @secondbegin table (id int,secondstarttime datetime)insert into @secondbeginselect 1,'2011-11-19 11:45:01.000' union allselect 2,'2011-12-09 11:45:01.000' union allselect 3,null--> 测试数据: @secondenddeclare @secondend table (id int,secondendtime datetime)insert into @secondendselect 1,'2011-11-30 11:45:01.000' union allselect 2,null union allselect 3,nullselect a.*,b.firstendtime,c.secondstarttime,d.secondendtime from @firstbegin a left join @firstend b on a.id=b.idleft join @secondbegin c on a.id=c.idleft join @secondend d on a.id=d.idwhere a.firststarttime between '2011-12-01' and '2011-12-10'/*id          firststarttime          firstendtime            secondstarttime         secondendtime----------- ----------------------- ----------------------- ----------------------- -----------------------2           2011-12-01 10:45:01.000 2011-12-08 10:45:01.000 2011-12-09 11:45:01.000 NULL3           2011-12-01 10:45:01.000 2011-12-09 10:45:01.000 NULL                    NULL*/
[解决办法]
探讨
引用:

SQL code

--> 测试数据: @firstbegin
declare @firstbegin table (id int,firststarttime datetime)
insert into @firstbegin
select 1,'2011-11-22 10:45:01.000' union all
select 2,……

热点排行