获得每月出勤率

时间:2018-02-15 10:06:53

标签: sql-server

我有一个像这样的字段和值的表

IF EXISTS(SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID('myTable'))
    BEGIN;
        DROP TABLE [myTable];
    END;
    GO

CREATE TABLE [myTable] (
    [myTableID] INTEGER NOT NULL IDENTITY(1, 1),
    [EmpID] INTEGER NULL,
    [Name] VARCHAR(255) NULL,
    [TimeIn] datetime,
    [TimeOut] datetime,
    [Status] VARCHAR(255) NULL,
    PRIMARY KEY ([myTableID])
);
GO

INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(1,'Emmanuel','2018-01-09 08:25:39',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(1,'Emmanuel',NULL,'2018-01-09 16:13:53','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(1,'Emmanuel','2018-01-11 08:25:39',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(1,'Emmanuel',NULL,'2018-01-11 11:13:53','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(1,'Emmanuel','2018-01-11 12:25:39',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(1,'Emmanuel',NULL,'2018-01-11 16:13:53','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(2,'Stone','2018-01-09 09:33:44',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(2,'Stone',NULL,'2018-01-09 16:33:44','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(4,'Basia','2018-01-31 15:08:07',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(4,'Basia',NULL,'2018-01-31 16:08:07','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(2,'Stone','2018-01-19 09:33:44',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(2,'Stone',NULL,'2018-01-19 16:33:44','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(4,'Basia','2018-01-11 15:08:07',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(4,'Basia',NULL,'2018-01-11 16:08:07','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(5,'Nathan','2018-01-19 08:55:22',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(5,'Nathan',NULL,'2018-01-19 17:46:32','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(5,'Nathan','2018-01-12 08:55:22',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(5,'Nathan',NULL,'2018-01-12 17:46:32','Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(5,'Nathan','2018-01-08 08:55:22',NULL,'Present');
INSERT INTO myTable([EmpID],[Name],[TimeIn],[TimeOut],[Status]) VALUES(5,'Nathan',NULL,'2018-01-08 17:46:32','Present');

我正在努力获得员工在公司工作的天数。这是我到目前为止所尝试的

DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2018-01-01'
SET @EndDate = '2018-01-31'

SELECT EmpID, COUNT(*) AS DaysWorked
FROM myTable 
WHERE CAST([TimeIn] AS DATE) >= @StartDate AND CAST([TimeIn] AS DATE) <= @EndDate
GROUP BY EmpID

结果我看起来像这样。但是Emmanuel应该工作2天,因为他早上来到中午,然后又回来了,所以应该算作一天。

EmpID       DaysWorked
----------- -----------
1           3
2           2
4           2
5           3

1 个答案:

答案 0 :(得分:2)

这应该适合你。

EmpId   DaysWorked
1         2
2         2
4         2
5         3

输出:

span