题目是这样的:原始刷卡记录是
工号 日期 时间
A 7月1日 07:41
A 7月1日 11:31
A 7月1日 13:20
A 7月1日 17:44
A 7月1日 18:10
A 7月1日 20:30
B 7月1日 07:41
B 7月1日 11:31
B 7月1日 13:20
B 7月1日 17:44
B 7月1日 18:10
B 7月1日 20:30
结果是:
工号 日期 时间1 时间2 时间3 时间4 时间5 时间6 工时
A 7月1日 07:41 11:31 13:20 17:44 18:10 20:30 10
B 7月1日 07:41 11:31 13:20 17:44 18:10 20:30 10
要求:通过存储过程,可以输入工号和日期来统计。
请各位帮我重头到尾给我讲述一遍好吗,有些字段最好能帮我解释一下,在这先谢谢各位
工号 日期 时间
A 7月1日 07:41
A 7月1日 11:31
A 7月1日 13:20
A 7月1日 17:44
A 7月1日 18:10
A 7月1日 20:30
B 7月1日 07:41
B 7月1日 11:31
B 7月1日 13:20
B 7月1日 17:44
B 7月1日 18:10
B 7月1日 20:30
结果是:
工号 日期 时间1 时间2 时间3 时间4 时间5 时间6 工时
A 7月1日 07:41 11:31 13:20 17:44 18:10 20:30 10
B 7月1日 07:41 11:31 13:20 17:44 18:10 20:30 10
要求:通过存储过程,可以输入工号和日期来统计。
请各位帮我重头到尾给我讲述一遍好吗,有些字段最好能帮我解释一下,在这先谢谢各位
insert into tb values('A','7月1日','11:31')
insert into tb values('A','7月1日','13:20')
insert into tb values('A','7月1日','17:44')
insert into tb values('A','7月1日','18:10')
insert into tb values('A','7月1日','20:30')
insert into tb values('B','7月1日','07:41')
insert into tb values('B','7月1日','11:31')
insert into tb values('B','7月1日','13:20')
insert into tb values('B','7月1日','17:44')
insert into tb values('B','7月1日','18:10')
insert into tb values('B','7月1日','20:30')declare @sql varchar(8000)
set @sql = 'select 工号,日期'
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then 时间 end) [时间' + cast(px as varchar) + ']'
from (select distinct px from (select px=(select count(1) from tb where 工号=a.工号 and 日期=a.日期 and 时间<a.时间)+1 , * from tb a) t) as m
set @sql = @sql + ' from (select px=(select count(1) from tb where 工号=a.工号 and 日期=a.日期 and 时间<a.时间)+1 , * from tb a) t group by 工号,日期'
exec(@sql) drop table tb/*
工号 日期 时间1 时间2 时间3 时间4 时间5 时间6
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
A 7月1日 07:41 11:31 13:20 17:44 18:10 20:30
B 7月1日 07:41 11:31 13:20 17:44 18:10 20:30
*/
insert into t select 'A','7月1日','07:41'
insert into t select 'A','7月1日','11:31'
insert into t select 'A','7月1日','13:20'
insert into t select 'A','7月1日','17:44'
insert into t select 'A','7月1日','18:10'
insert into t select 'A','7月1日','20:30'
insert into t select 'B','7月1日','07:41'
insert into t select 'B','7月1日','11:31'
insert into t select 'B','7月1日','13:20'
insert into t select 'B','7月1日','17:44'
insert into t select 'B','7月1日','18:10'
insert into t select 'B','7月1日','20:30'
gocreate procedure sp_test
as
begin
select
(select count(*) from t where 工号=a.工号 and 日期=a.日期 and 时间<=a.时间) as id,a.*
into #
from
t a select
a.工号,a.日期,
max(case id when 1 then 时间 end) as 时间1,
max(case id when 2 then 时间 end) as 时间2,
max(case id when 3 then 时间 end) as 时间3,
max(case id when 4 then 时间 end) as 时间4,
max(case id when 5 then 时间 end) as 时间5,
max(case id when 6 then 时间 end) as 时间6,
(datediff(mi,max(case id when 1 then 时间 end),max(case id when 2 then 时间 end))+
datediff(mi,max(case id when 3 then 时间 end),max(case id when 4 then 时间 end))+
datediff(mi,max(case id when 5 then 时间 end),max(case id when 6 then 时间 end)))/60 as 工时
from
# a
group by
a.工号,a.日期
end
go
exec sp_test
go工号 日期 时间1 时间2 时间3 时间4 时间5 时间6 工时
-------- -------- -------- -------- -------- -------- -------- -------- -----------
A 7月1日 07:41 11:31 13:20 17:44 18:10 20:30 10
B 7月1日 07:41 11:31 13:20 17:44 18:10 20:30 10
drop procedure sp_test
drop table t
go
update fpc_yssk set sj2 =(select min(rtime) from fpc_workrecord a where a.rno = fpc_yssk.rno and a.rdate = fpc_yssk.rdate and rtime > dateadd(mi,2,fpc_yssk.sj1))
update fpc_yssk set sj3 =(select min(rtime) from fpc_workrecord a where a.rno = fpc_yssk.rno and a.rdate = fpc_yssk.rdate and rtime > dateadd(mi,2,fpc_yssk.sj2))
update fpc_yssk set sj4 =(select min(rtime) from fpc_workrecord a where a.rno = fpc_yssk.rno and a.rdate = fpc_yssk.rdate and rtime > dateadd(mi,2,fpc_yssk.sj3))
update fpc_yssk set sj5 =(select min(rtime) from fpc_workrecord a where a.rno = fpc_yssk.rno and a.rdate = fpc_yssk.rdate and rtime > dateadd(mi,2,fpc_yssk.sj4))
update fpc_yssk set sj6 =(select min(rtime) from fpc_workrecord a where a.rno = fpc_yssk.rno and a.rdate = fpc_yssk.rdate and rtime > dateadd(mi,2,fpc_yssk.sj5))这样按最小的也可以吧