日期:2014-05-17 浏览次数:20433 次
declare @a table(
empno varchar(20),
begin_date datetime,
begin_time char(5),
add_miniute int,
min_val varchar(50),
max_val varchar(50)
)
declare @b table(
empno varchar(20),
val_date datetime,
val_time char(5)
)
insert into @a
select 'x1','2013-05-01','08:00',60,'',''
union
select 'x2','2013-05-01','23:00',300,'',''
insert into @b
select 'x1','2013-05-01','07:00'
union
select 'x1','2013-05-01','08:00'
union
select 'x1','2013-05-01','09:00'
union
select 'x1','2013-05-01','10:00'
union
select 'x2','2013-05-01','23:15'
union
select 'x2','2013-05-02','01:00'
union
select 'x2','2013-05-02','02:15'
select a.empno,a.begin_date,a.begin_time,add_miniute,min(convert(nvarchar(11),b.val_date,120) + b.val_time) as min_val,
max(convert(nvarchar(11),b.val_date,120) + b.val_time) as max_val
from @a a,@b b
where a.empno = b.empno
and convert(datetime,convert(nvarchar(11),b.val_date,120) + b.val_time)
between convert(datetime,convert(nvarchar(11),a.begin_date,120) +a.begin_time)
and dateadd(mi,add_miniute,convert(datetime,convert(nvarchar(11),a.begin_date,120) +a.begin_time))
group by a.empno,a.begin_date,a.begin_time,a.add_miniute
/*
empno,begin_date,begin_time,add_miniute,min_val,max_val
x1,2013-05-01 00:00:00.000,08:00,60,2013-05-01 08:00,2013-05-01 09:00
x2,2013-05-01 00:00:00.000,23:00,300,2013-05