求教SqlServer如何查询不同状态的同时间字段的实际差

求教各位大佬,我现在要对一个签到表进行每个人的工作时间的统计,表如下

图片说明
其中sign_type (1234)分别代表上午签到和签退 下午签到和签退 四种标记

每次传入trainingid为条件,来查询这个人的总共工作时间,谢谢了

如果能考虑上漏签不算就更好了

给你讲讲我的思路,首先将传入的trainingid为条件,将这个表拆分成四个表,分别为上午签到表,上午签退表,下午签到表,下午签退表,然后分别查出上午的工作时间,下午的工作时间相加就能得到你要的结果。

你要考虑打表的时候
1.是否会重复的打几次(导致某一天有sign_type重复的数据 _)
2.是否会忘记打卡(导致缺少sign_type 某一个数据)

对于考虑一就该 获取时间最早的打卡记录
对于考虑二就该 用特定的时间长度来代替 比如上午给他算俩个小时

假如training_id为a123456a_ 时间是2017-09-19
现在求某个某一天的的工作时间(假设他这天不是当天:即这天已经过了24小时)

select count(time) alltime from
(
select (case max( a.creditcard_date)-min(a.creditcard_date) when 0 then 1/12 end)*24 /*...这里有未打卡处理...*/

time from
/*...数据表开始...*/
(select *
(select sign_type,creditcard_date from TABLE where
CONVERT(varchar(100), creditcard_date, 23)= '2017-09-19'
and training_id = 'a123456a') t
where not exists (select 1 from (select sign_type,creditcard_date
from TABLE where
CONVERT(varchar(100), creditcard_date, 23)= '2017-09-19'
and training_id = 'a123456a') b where b.sign_type = t.sign_type /*...这里有重复数据处理...*/
and creditcard_date > t.creditcard_date)) a

/*...数据表结束...*/
where sign_type in (1,2)

union all

select (case max( a.creditcard_date)-min(a.creditcard_date) when 0 then 1/12 end)*24

time from

/*...数据表开始...*/
(select *
(select sign_type,creditcard_date from TABLE where
CONVERT(varchar(100), creditcard_date, 23)= '2017-09-19'
and training_id = 'a123456a') t
where not exists (select 1 from (select sign_type,creditcard_date
from TABLE where
CONVERT(varchar(100), creditcard_date, 23)= '2017-09-19'
and training_id = 'a123456a') b where b.sign_type = t.sign_type
and creditcard_date > t.creditcard_date)) a

/*...数据表结束...*/

where sign_type in (3,4)

)_temp