2

我的表结构为

TblID | EmpID |   checkin time | check state
---------------------------------------------
17902   2   2013-06-08 09:43:37.000 1
17951   2   2013-06-09 07:28:58.000 0
17989   2   2013-06-09 15:40:26.000 1
17991   2   2013-06-09 16:28:35.000 0
18008   2   2013-06-09 21:20:13.000 1
18009   2   2013-06-10 11:00:13.000 1
18217   2   2013-06-12 20:11:17.000 0
18247   2   2013-06-13 08:30:19.000 0
18248   2   2013-06-13 08:30:45.000 1

我只需要按日期分组为 48 小时,结果将是

Date       | Time 
----------------------------    
2013-06-08  2013-06-08 09:43 
2013-06-08  2013-06-09 07:28
2013-06-08  2013-06-09 15:40 
2013-06-08  2013-06-09 16:28
2013-06-08  2013-06-09 21:20

2013-06-09  2013-06-09 07:28
2013-06-09  2013-06-09 15:40 
2013-06-09  2013-06-09 16:28
2013-06-09  2013-06-09 21:20
2013-06-09  2013-06-10 11:00  

如果我的方法是错误的,当我让员工在一天内签到并在第二天签出时,你能否建议另一种计算出勤率的方法。

4

1 回答 1

2

如果您的 SQL Server 是 2008 或更高版本,请尝试以下查询:

SELECT DISTINCT CAST(M1.[checkin time] AS Date), M2.[checkin time] from MyTable M1
INNER JOIN MyTable M2 
    ON M2.Date BETWEEN CAST(M1.[checkin time] AS Date) AND DATEADD(d, 1, CAST(M1.[checkin time] AS Date))

在 SQL Server 2005 或更低版本上:

SELECT DISTINCT DATEADD(D, 0, DATEDIFF(D, 0, M1.[checkin time])), M2.[checkin time] from MyTable M1
INNER JOIN MyTable M2 
    ON M2.Date BETWEEN DATEADD(D, 0, DATEDIFF(D, 0, M1.[checkin time])) AND DATEADD(d, 1, DATEADD(D, 0, DATEDIFF(D, 0, M1.[checkin time])))
于 2013-10-06T00:45:01.193 回答