想象一下,如果我们可以轻松获得数据,我们就能清楚地理解预期的最终结果应该是什么样子。
SQL Fiddle
的MS SQL Server 2014架构设置:
CREATE TABLE Accesslog
([EmployeeID] int, [LogDate] datetime, [LogTime] datetime, [Terminal] varchar(11), [InOut] int, [Accepted] int)
;
INSERT INTO Accesslog
([EmployeeID], [LogDate], [LogTime], [Terminal], [InOut], [Accepted])
VALUES
(1009, '2015-10-14 00:00:00', '1900-01-01 06:36:06', 'abcdedghijk', 0, 1),
(1009, '2015-10-14 00:00:00', '1900-01-01 16:22:41', 'abcdedghijk', 1, 1)
;
查询1:
select
EmployeeID
, LogDate
, MIN(case when InOut = 0 then cast(LogTime as time) end) as LogTimeMin
, MAX(case when InOut = 1 then cast(LogTime as time) end) as LogTimeMax
, COUNT(*) as CountPunches
, CONVERT(varchar(12),
DATEADD(minute,
DATEDIFF(minute,
MIN(case when InOut = 0 then cast(LogTime as time) end),
MAX(case when InOut = 1 then cast(LogTime as time) end)), 0), 114) as HoursAttending
from Accesslog
group by
EmployeeID
, LogDate
Results:
| EmployeeID | LogDate | LogTimeMin | LogTimeMax | CountPunches | HoursAttending |
|------------|------------------|-------------|-------------|--------------|----------------|
| 1009 | October, 14 2015 | 06:36:06.00 | 16:22:41.00 | 2 | 09:46:00:000 |
PART 2
为了解决偏移的问题,请尝试以下方法。请注意,如果你再想每天只有一个排,使用一组通过查询很好,但做下面的查询看到成“派生表”(子查询)
SQL Fiddle
MS SQL服务器2014架构设置 :
CREATE TABLE Accesslog
([EmployeeID] int, [LogDate] datetime, [LogTime] datetime, [Terminal] varchar(11), [InOut] int, [Accepted] int)
;
INSERT INTO Accesslog
([EmployeeID], [LogDate], [LogTime], [Terminal], [InOut], [Accepted])
VALUES
(1209, '2015-10-14 00:00:00', '1900-01-01 07:00:00', 'abcdedghijk', 0, 1),
(1209, '2015-10-14 00:00:00', '1900-01-01 12:01:00', 'abcdedghijk', 1, 1),
(1209, '2015-10-14 00:00:00', '1900-01-01 15:00:00', 'abcdedghijk', 0, 1),
(1209, '2015-10-14 00:00:00', '1900-01-01 20:02:00', 'abcdedghijk', 1, 1),
(1009, '2015-10-14 00:00:00', '1900-01-01 08:00:00', 'abcdedghijk', 0, 1),
(1009, '2015-10-14 00:00:00', '1900-01-01 16:00:00', 'abcdedghijk', 1, 1),
(1088, '2015-10-15 00:00:00', '1900-01-01 09:00:00', 'aaaa', 0, 1),
(1088, '2015-10-15 00:00:00', '1900-01-01 15:00:00', 'aaaa', 1, 1)
;
查询1:
/*
including (07:00 - 12:00) shift two (15:00 - 20:00)
*/
WITH CTE as (
SELECT *, ROW_NUMBER() OVER(PARTITION BY EmployeeID, LogDate, InOut
ORDER BY LogTime ASC) AS shiftno
FROM Accesslog
)
SELECT
ins.EmployeeID
, ins.LogDate
, ins.LogTime as LogTimeIn
, outs.LogTime as LogTimeOut
, ins.Accepted + ISNULL(outs.Accepted,0) as CountPunches
, CONVERT(varchar(12),
DATEADD(minute,
DATEDIFF(minute,
ins.LogTime,
outs.LogTime), 0), 114) as HoursAttending
FROM CTE AS ins
INNER JOIN CTE AS outs ON ins.InOut = 0 AND outs.InOut = 1
AND ins.EmployeeID = outs.EmployeeID
AND ins.LogDate = outs.LogDate
AND ins.shiftno = outs.shiftno
Results:
| EmployeeID | LogDate | LogTimeIn | LogTimeOut | CountPunches | HoursAttending |
|------------|---------------------------|---------------------------|---------------------------|--------------|----------------|
| 1009 | October, 14 2015 00:00:00 | January, 01 1900 08:00:00 | January, 01 1900 16:00:00 | 2 | 08:00:00:000 |
| 1088 | October, 15 2015 00:00:00 | January, 01 1900 09:00:00 | January, 01 1900 15:00:00 | 2 | 06:00:00:000 |
| 1209 | October, 14 2015 00:00:00 | January, 01 1900 07:00:00 | January, 01 1900 12:01:00 | 2 | 05:01:00:000 |
| 1209 | October, 14 2015 00:00:00 | January, 01 1900 15:00:00 | January, 01 1900 20:02:00 | 2 | 05:02:00:000 |
你所说的 '不工作' 是什么意思? –
@vkp我的意思是只有HoursAttending列在每行中取一个值。我将更新结果图片的帖子 – Loai
您在子查询 – Horaciux