每周出勤总结一次

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每周出勤总结一次相关的知识,希望对你有一定的参考价值。

问候所有, 我想知道我是否能获得一些关于获得考勤数据的最佳方式的见解。 我需要在一天内获得所有可行的(AttendanceTypeID = 1)和out(AttendanceTypeID = 2)对。从周日开始到周六结束,按周计算每位员工的时间值。进入外部的壁橱是所需要的业务规则,并且在一天内可能有许多进/出对。我也使用SQL 2008逻辑,因为我们的一些客户仍在使用SQL 2008.我已经看到了一个使用CTE的视图示例,我更喜欢,但他们使用了从2012年开始的lead,lag,LastVal,NestVal函数。 输出。

https://forums.asp.net/t/1946217.aspx?Time+Attendance+working+hours https://www.red-gate.com/simple-talk/sql/t-sql-programming/solving-complex-t-sql-problems,-step-by-step/

CREATE TABLE tblAttendance
(
[AttendanceID] int, 
[DepartmentID] int, 
[EmployeeID] int, 
[AttendanceDate] datetime, 
[AttendanceTime] varchar(8), 
[AttendanceTypeID] int, 
[AttendanceCodeID] int, 
[Submitted] int
);



INSERT INTO tblAttendance
    ([AttendanceID], [DepartmentID], [EmployeeID], [AttendanceDate], 
[AttendanceTime], [AttendanceTypeID], [AttendanceCodeID], [Submitted])
    VALUES
        (838, 33, 260, '2018-02-26 00:00:00', '8:00:00', 1, 1, 0),
        (839, 33, 260, '2018-02-26 00:00:00', '22:00:00', 2, 1, 0),
        (836, 41, 344, '2018-02-26 00:00:00', '9:00:00', 1, 1, 0),
        (837, 41, 344, '2018-02-26 00:00:00', '22:00:00', 2, 1, 0),
        (812, 33, 348, '2018-02-26 00:00:00', '8:00:00', 1, 1, 0),
        (813, 33, 348, '2018-02-26 00:00:00', '12:00:00', 2, 1, 0),
        (814, 33, 350, '2018-02-26 00:00:00', '8:00:00', 1, 1, 0),
        (815, 33, 350, '2018-02-26 00:00:00', '12:00:00', 2, 1, 0),
        (930, 7, 361, '2018-02-26 00:00:00', '7:00:00', 1, 1, 0),
        (931, 7, 361, '2018-02-26 00:00:00', '9:00:00', 2, 1, 0),
        (940, 19, 361, '2018-02-26 00:00:00', '8:55:00', 1, 1, 0),
        (941, 19, 361, '2018-02-26 00:00:00', '10:00:00', 2, 1, 0),
        (824, 1, 114, '2018-02-27 00:00:00', '23:59:57', 2, 1, 0),
        (816, 33, 206, '2018-02-27 00:00:00', '11:12:00', 1, 1, 0),
        (819, 33, 206, '2018-02-27 00:00:00', '11:16:00', 2, 1, 0),
        (822, 1, 350, '2018-02-27 00:00:00', '0:00:00', 1, 1, 0),
        (829, 33, 350, '2018-02-27 00:00:00', '16:15:30', 1, 1, 0),
        (830, 33, 359, '2018-02-27 00:00:00', '16:15:30', 1, 1, 0),
        (932, 7, 361, '2018-02-27 00:00:00', '7:00:00', 1, 1, 0),
        (933, 7, 361, '2018-02-27 00:00:00', '9:00:00', 2, 1, 0),
        (942, 19, 361, '2018-02-27 00:00:00', '8:30:00', 1, 1, 0),
        (943, 19, 361, '2018-02-27 00:00:00', '10:30:00', 2, 1, 0),
        (835, 33, 206, '2018-02-28 00:00:00', '9:23:00', 1, 1, 0),
        (934, 7, 361, '2018-02-28 00:00:00', '7:00:00', 1, 1, 0),
        (935, 7, 361, '2018-02-28 00:00:00', '9:00:00', 2, 1, 0),
        (944, 19, 361, '2018-02-28 00:00:00', '7:00:00', 1, 1, 0),
        (945, 19, 361, '2018-02-28 00:00:00', '9:00:00', 2, 1, 0),
        (936, 7, 361, '2018-03-01 00:00:00', '7:00:00', 1, 1, 0),
        (937, 7, 361, '2018-03-01 00:00:00', '9:00:00', 2, 1, 0),
        (946, 19, 361, '2018-03-01 00:00:00', '9:00:00', 1, 1, 0),
        (947, 19, 361, '2018-03-01 00:00:00', '9:30:00', 2, 1, 0),
        (840, 33, 350, '2018-03-02 00:00:00', '8:21:00', 1, 1, 0),
        (841, 33, 350, '2018-03-02 00:00:00', '8:22:00', 2, 1, 0),
        (938, 7, 361, '2018-03-02 00:00:00', '7:00:00', 1, 1, 0),
        (939, 7, 361, '2018-03-02 00:00:00', '9:00:00', 2, 1, 0),
        (948, 19, 361, '2018-03-02 00:00:00', '9:01:00', 1, 1, 0),
        (949, 19, 361, '2018-03-02 00:00:00', '10:00:00', 2, 1, 0),
        (894, 33, 260, '2018-03-05 00:00:00', '6:00:00', 1, 1, 0),
        (895, 33, 260, '2018-03-05 00:00:00', '7:00:00', 2, 1, 0),
        (859, 33, 348, '2018-03-05 00:00:00', '9:30:00', 1, 1, 0),
        (860, 33, 348, '2018-03-05 00:00:00', '9:30:00', 1, 1, 0),
        (842, 33, 206, '2018-03-06 00:00:00', '13:21:00', 1, 1, 0),
        (856, 33, 206, '2018-03-06 00:00:00', '2:51:27', 2, 1, 0),
        (848, 33, 206, '2018-03-06 00:00:00', '13:57:00', 2, 1, 0),
        (843, 33, 260, '2018-03-06 00:00:00', '14:00:00', 1, 1, 0),
        (861, 33, 348, '2018-03-06 00:00:00', '9:30:00', 1, 1, 0),
        (862, 33, 348, '2018-03-06 00:00:00', '9:31:00', 1, 1, 0),
        (853, 33, 350, '2018-03-06 00:00:00', '2:32:36', 1, 1, 0),
        (854, 33, 350, '2018-03-06 00:00:00', '2:48:54', 1, 1, 0),
        (855, 33, 350, '2018-03-06 00:00:00', '2:49:11', 1, 1, 0),
        (857, 33, 350, '2018-03-06 00:00:00', '2:52:19', 1, 1, 0),
        (844, 33, 350, '2018-03-06 00:00:00', '13:32:00', 1, 1, 0),
        (846, 33, 350, '2018-03-06 00:00:00', '13:53:00', 1, 1, 0),
        (845, 33, 350, '2018-03-06 00:00:00', '13:35:00', 2, 1, 0),
        (850, 33, 350, '2018-03-06 00:00:00', '14:06:00', 2, 1, 0),
        (847, 33, 359, '2018-03-06 00:00:00', '13:56:00', 1, 1, 0),
        (858, 33, 359, '2018-03-06 00:00:00', '14:52:00', 1, 1, 0),
        (852, 33, 359, '2018-03-06 00:00:00', '2:29:06', 2, 1, 0),
        (851, 33, 359, '2018-03-06 00:00:00', '14:09:00', 2, 1, 0),
        (896, 33, 260, '2018-03-07 00:00:00', '6:00:00', 1, 1, 0),
        (897, 33, 260, '2018-03-07 00:00:00', '7:00:00', 2, 1, 0),
        (907, 33, 348, '2018-03-07 00:00:00', '7:00:00', 1, 1, 0),
        (908, 33, 348, '2018-03-07 00:00:00', '8:00:00', 2, 1, 0),
        (893, 33, 350, '2018-03-07 00:00:00', '14:36:40', 1, 1, 0),
        (915, 33, 350, '2018-03-07 00:00:00', '17:00:00', 2, 1, 0),
        (913, 33, 359, '2018-03-07 00:00:00', '8:00:00', 1, 1, 0),
        (914, 33, 359, '2018-03-07 00:00:00', '17:00:00', 2, 1, 0),
        (923, 33, 348, '2018-03-08 00:00:00', '7:00:00', 1, 1, 0),
        (925, 33, 348, '2018-03-08 00:00:00', '11:00:00', 1, 1, 0),
        (928, 33, 348, '2018-03-08 00:00:00', '11:45:00', 1, 1, 0),
        (927, 33, 348, '2018-03-08 00:00:00', '11:35:00', 2, 1, 0),
        (924, 33, 348, '2018-03-08 00:00:00', '12:00:00', 2, 1, 0),
        (926, 33, 348, '2018-03-08 00:00:00', '13:00:00', 2, 1, 0),
        (898, 33, 350, '2018-03-08 00:00:00', '11:42:57', 1, 1, 0),
        (906, 33, 206, '2018-03-09 00:00:00', '8:14:43', 1, 1, 0),
        (905, 33, 260, '2018-03-09 00:00:00', '8:12:53', 1, 1, 0),
        (972, 33, 348, '2018-03-09 00:00:00', '8:00:00', 1, 1, 0),
        (911, 33, 350, '2018-03-09 00:00:00', '10:43:59', 1, 1, 0),
        (912, 33, 350, '2018-03-09 00:00:00', '10:44:22', 1, 1, 0),
        (917, 33, 350, '2018-03-09 00:00:00', '12:44:48', 1, 1, 0),
        (929, 33, 350, '2018-03-09 00:00:00', '15:18:17', 1, 1, 0),
        (921, 33, 350, '2018-03-09 00:00:00', '12:50:14', 2, 1, 0),
        (918, 33, 359, '2018-03-09 00:00:00', '12:44:48', 1, 1, 0),
        (922, 33, 359, '2018-03-09 00:00:00', '12:50:14', 2, 1, 0),
        (970, 33, 350, '2018-03-11 00:00:00', '11:25:00', 1, 1, 0),
        (968, 33, 260, '2018-03-12 00:00:00', '7:00:00', 1, 1, 0),
        (962, 33, 348, '2018-03-12 00:00:00', '7:00:00', 1, 1, 0),
        (969, 33, 348, '2018-03-12 00:00:00', '9:00:00', 2, 1, 0),
        (951, 33, 350, '2018-03-12 00:00:00', '8:54:07', 1, 1, 0),
        (953, 33, 350, '2018-03-12 00:00:00', '10:31:29', 1, 1, 0),
        (954, 33, 350, '2018-03-12 00:00:00', '10:34:18', 1, 1, 0),
        (950, 33, 350, '2018-03-12 00:00:00', '8:21:11', 2, 1, 0),
        (952, 33, 350, '2018-03-12 00:00:00', '9:02:26', 2, 1, 0),
        (955, 33, 359, '2018-03-12 00:00:00', '10:37:26', 1, 1, 0),
        (959, 33, 206, '2018-03-13 00:00:00', '1:01:38', 1, 1, 0),
        (961, 33, 206, '2018-03-13 00:00:00', '14:00:51', 1, 1, 0),
        (975, 33, 295, '2018-03-13 00:00:00', '7:00:00', 1, 1, 0),
        (976, 33, 295, '2018-03-13 00:00:00', '8:00:00', 2, 1, 0),
        (957, 33, 350, '2018-03-13 00:00:00', '11:26:52', 1, 1, 0),
        (958, 33, 359, '2018-03-13 00:00:00', '1:00:12', 1, 1, 0),
        (965, 33, 359, '2018-03-13 00:00:00', '21:00:00', 1, 1, 0),
        (960, 33, 359, '2018-03-13 00:00:00', '13:14:14', 2, 1, 0),
        (967, 33, 359, '2018-03-13 00:00:00', '22:00:00', 2, 1, 0),
        (964, 33, 350, '2018-03-14 00:00:00', '14:34:36', 2, 1, 0),
        (963, 33, 359, '2018-03-14 00:00:00', '13:45:25', 1, 1, 0)
    ;

这是我用来返回上面数据的查询:

SELECT  AttendanceID   , DepartmentID   , EmployeeID   , AttendanceDate   , AttendanceTime   , AttendanceTypeID   , AttendanceCodeID   , Submitted
FROM tblAttendance
WHERE AttendanceDate BETWEEN @StartDate AND @EndDate
AND AttendanceTypeID IN (1,2)  -- 1 = in, and 2 = out
AND AttendanceCodeID = 1
ORDER BY
AttendanceDate
,AttendanceTime

下面添加的表格显示了获得最终结果所需的步骤。首先是一个数据示例:

+------------+----------------+----------------+------------------+
| EmployeeID | AttendanceDate | AttendanceTime | AttendanceTypeID |
+------------+----------------+----------------+------------------+
| 350        | 3/6/18         | 2:32:36        |        1         |
| 350        | 3/6/18         | 2:48:54        |        1         |
| 350        | 3/6/18         | 2:49:11        |        1         |
| 350        | 3/6/18         | 2:52:19        |        1         |
| 350        | 3/6/18         | 13:32:00       |        1         |
| 350        | 3/6/18         | 13:53:00       |        1         |
| 350        | 3/6/18         | 13:35:00       |        2         |
| 350        | 3/6/18         | 14:06:00       |        2         |
| 350        | 3/7/18         | 14:36:40       |        1         |
| 350        | 3/7/18         | 17:00:00       |        2         |
| 350        | 3/8/18         | 11:42:57       |        1         |
| 350        | 3/9/18         | 10:43:59       |        1         |
| 350        | 3/9/18         | 10:44:22       |        1         |
| 350        | 3/9/18         | 12:44:48       |        1         |
| 350        | 3/9/18         | 15:18:17       |        1         |
| 350        | 3/9/18         | 12:50:14       |        2         |
| 350        | 3/9/18         | 10:43:59       |        1         |
| 350        | 3/9/18         | 10:44:22       |        1         |
| 350        | 3/9/18         | 12:44:48       |        1         |
| 350        | 3/9/18         | 15:18:17       |        1         |
| 350        | 3/9/18         | 12:50:14       |        2         |
+------------+----------------+----------------+------------------+

第二个将成对的匹配数据配对,并获得时差。

+------------+----------------+------------------+------------------+
| EmployeeID | AttendanceDate | AttendanceTime   | Pair Total       |
+------------+----------------+------------------+------------------+
| 350        | 3/6/18         |13:35:00 -13:32:00|     0:03:00      |
| 350        | 3/6/18         |14:06:00 -13:53:00|     0:13:00      |
| 350        | 3/7/18         |17:00:00 -14:36:40|     2:23:20      |
| 350        | 3/9/18         |12:44:48 -12:44:48|     0:05:26      |
+------------+----------------+------------------+------------------+

第三个是该周员工的编译数据。

+------------+-------------------+----------------------+
| EmployeeID | AttendanceWeek    | AttendanceTime Total | 
+------------+-------------------+----------------------+
| 350        | 3/4/18 to 3/10/18 |     2:44:46          |
+------------+-------------------+----------------------+

Hope that this helps narrow the focus.

谢谢。

答案

首先,您需要确保星期日是开始周的默认值。

--see what day is the default start day of the week
SELECT @@DATEFIRST
--set it to Sunday if value isn't 7
SET DATEFIRST 7;

我想我通过使用CTE,几个SUM CASE语句和DATEPART函数找出了你的问题。

DECLARE @StartDate DATETIME = '2/25/2018 12:00AM';
DECLARE @EndDate DATETIME = '3/17/2018 11:59PM';

--Count number of 1's and 2's in a day
--Group by EmployeeId and AttendanceDate
WITH daily1And2 AS(
    SELECT EmployeeID,
       AttendanceDate,
           SUM(CASE WHEN AttendanceTypeID = 1 THEN 1 ELSE 0 END) 'Num1',
           SUM(CASE WHEN AttendanceTypeID = 2 THEN 1 ELSE 0 END) 'Num2'
    FROM tblAttendance
    WHERE AttendanceDate BETWEEN @StartDate AND @EndDate
          AND AttendanceTypeID IN (1,2)
          AND AttendanceCodeID = 1
    GROUP BY EmployeeID,AttendanceDate)

--Find the number of pairs and group by week
SELECT EmployeeID,
       DATEPART(wk,AttendanceDate)'Week', --Get Week
       --Getting the lowest of the 2 numbers, because that will
       --be the number of pairs, then summing for the week
       SUM(CASE WHEN Num1 > Num2 THEN Num2 ELSE Num1 END) 'Num1and2Pairs'
FROM daily1And2
GROUP BY EmployeeID,DATEPART(wk,AttendanceDate)
ORDER BY EmployeeID,DATEPART(wk,AttendanceDate)

输出:

+------------+------+---------------+
| EmployeeID | Week | Num1and2Pairs |
+------------+------+---------------+
|     114    |   9  |       0       |
|     206    |   9  |       1       |
|     206    |  10  |       1       |
|     206    |  11  |       0       |
|     260    |   9  |       1       |
|     260    |  10  |       2       |
|     260    |  11  |       0       |
|     295    |  11  |       1       |
|     344    |   9  |       1       |
|     348    |   9  |       1       |
|     348    |  10  |       4       |
|     348    |  11  |       1       |
|     350    |   9  |       2       |
|     350    |  10  |       4       |
|     350    |  11  |       2       |
|     359    |   9  |       0       |
|     359    |  10  |       4       |
|     359    |  11  |       2       |
|     361    |   9  |      10       |
+------------+------+---------------+

以上是关于每周出勤总结一次的主要内容,如果未能解决你的问题,请参考以下文章

算法千题案例每日LeetCode打卡——83.学生出勤记录 I

算法千题案例每日LeetCode打卡——83.学生出勤记录 I

每周总结①

2019目标!

《结对-蓝牙考勤程序-结对项目总结》

华为OD机试 - 考勤信息(Java) | 机试题+算法思路+考点+代码解析 2023