Sql 状态为空
首先这是检查表Sql 状态为空,sql,sql-server-2008,Sql,Sql Server 2008,首先这是检查表 select userid , checktime from checkinout where USERID=467 and CHECKTIME>'2014-10-24 00:00:00.000' order by chcktime userid checktime 467 2014-10-24 02:18:20.000 467 2014-10-24 17:04:43.000 467 2014-10-25 02:31:40.000
select userid , checktime from checkinout where
USERID=467 and CHECKTIME>'2014-10-24 00:00:00.000' order by chcktime
userid checktime
467 2014-10-24 02:18:20.000
467 2014-10-24 17:04:43.000
467 2014-10-25 02:31:40.000
467 2014-10-25 17:13:38.000
467 2014-10-25 20:59:10.000
467 2014-10-28 02:55:57.000
467 2014-10-28 17:57:12.000
467 2014-10-29 17:13:35.000
467 2014-10-30 02:35:47.000
467 2014-10-30 17:17:56.000
467 2014-10-31 03:02:50.000
467 2014-10-31 17:19:18.000
467 2014-11-01 02:42:18.000
467 2014-11-01 17:16:14.000
467 2014-11-02 02:44:00.000
467 2014-11-03 17:20:10.000
467 2014-11-04 02:38:57.000
467 2014-11-04 17:33:54.000
467 2014-11-05 02:15:50.000
467 2014-11-05 17:14:25.000
我有一个疑问
SELECT USERID,MIN(checktime) AS InTime,MAX(checktime) AS OutTime
FROM checkinout
where USERID=467 and CHECKTIME>'2014-10-24 00:00:00.000'
GROUP BY USERID,DATEDIFF(dd,0,DATEADD(hh,-4,checktime))
HAVING MIN(checktime) <> MAX(checktime)
AND MAX(DATEADD(hh,-4,checktime))-DATEDIFF(dd,0,DATEADD(hh,-4,checktime)) > '22:00'
ORDER BY MIN(checktime)
userid InTime OutTime
467 2014-10-24 17:04:43.000 2014-10-25 02:31:40.000
467 2014-10-29 17:13:35.000 2014-10-30 02:35:47.000
467 2014-10-30 17:17:56.000 2014-10-31 03:02:50.000
467 2014-10-31 17:19:18.000 2014-11-01 02:42:18.000
467 2014-11-01 17:16:14.000 2014-11-02 02:44:00.000
467 2014-11-03 17:20:10.000 2014-11-04 02:38:57.000
467 2014-11-04 17:33:54.000 2014-11-05 02:15:50.000
此表用于指纹签入和签出,用户id 467从下午5点切换到凌晨2点,这在您当前的数据结构中是不可能的 您当前的表是来自
userid->checktime
的关系,现在您希望以某种方式将checktime
拆分为intime
和outtime
,但如果没有更多信息,这是不可能的
你想按天分组,没关系。如果您在一天内找到2个checktime
条目,您就知道较低的条目是intime
,较高的条目是outtime
。但是如果只有一个checktime
条目,您就无法真正判断它是in
还是out
。唯一的办法是说“如果是单一的checktime
并且在12:00:00之前,那么它必须是intime
。如果是12:00:00之后,它必须是outtime
”
换句话说:如果给定的一天有一个checktime
条目,您将如何决定intime=null
或outtime=null
另一点是:你如何处理一天内3个(或更多)的检查时间
这里有一个快速解决方案(未经测试,但我希望您能理解):
选择USERID
,MIN(转换(时间,检查时间)>12:00:00然后检查时间结束时的情况)为初始时间
,最大值(转换时的情况)(时间,检查时间)'2014-10-24 00:00:00.000'
按用户ID、DATEDIFF(dd、0、DATEADD(hh、-4、checktime))分组
2号订单
Mr@Benjamin M让我们说,下午4点到8点之间的第一个签入时间为intime,最后一个签入时间为凌晨1点到凌晨4点之间的签入时间为outime,但是如果我们有intime而没有Outtime,那么我们能做到这一点吗
userid InTime OutTime
467 Null 2014-10-24 02:18:20.000
467 2014-10-24 17:04:43.000 2014-10-25 02:31:40.000
467 2014-10-25 17:13:38.000 Null
467 2014-10-25 20:59:10.000 Null
467 2014-10-28 17:57:12.000 Null
467 Null 2014-10-28 02:55:57.000
467 2014-10-29 17:13:35.000 2014-10-30 02:35:47.000
467 2014-10-30 17:17:56.000 2014-10-31 03:02:50.000
467 2014-10-31 17:19:18.000 2014-11-01 02:42:18.000
467 2014-11-01 17:16:14.000 2014-11-02 02:44:00.000
467 2014-11-03 17:20:10.000 2014-11-04 02:38:57.000
467 2014-11-04 17:33:54.000 2014-11-05 02:15:50.000
Select USERID
,MIN(Case When convert(time, checktime) > '12:00:00' Then checktime End) AS InTime
,MAX(Case When convert(time, checktime) <= '12:00:00' Then checktime End) AS OutTime
From checkinout
Where USERID=467 and CHECKTIME>'2014-10-24 00:00:00.000'
Group By USERID, DATEDIFF(dd,0,DATEADD(hh,-4,checktime))
Order By 2