日期:2014-05-18 浏览次数:20575 次
create table a([No] varchar(10),Date varchar(10),[Time] varchar(10))
insert into a
select '001', '20111230', '19:15'
union all select
'001' ,'20111231', '03:15'
union all select
'001', '20111231', '03:16'
union all select
'001', '20111229' ,'19:16'
union all select
'001' ,'20111230', '03:10'
select * from a
select isnull(a.No,b.No) No,isnull(a.Date,B.Date) Date,isnull(a.Time,'') WorkS,isnull(b.Time,'') WorkE,'夜班' 班次
from
(select No,Date,min(Time) Time from a where Time between '19:00' and '19:25' group by No,Date ) a full join
(select No,Date,min(Time) Time from a where Time between '03:00' and '03:25' group by No,Date ) b
on a.No=b.No and a.Date=dateadd(d,-1,b.Date)
--班次应该有一个排班表吧,这样的话就在上面的语句上加一个条件
--where exists(select 1 from 排班表 where 班次='夜班' and ( No=a.No or No=b.No )
/*
001 20111229 19:16 03:10 夜班
001 20111230 19:15 03:15 夜班
*/