Home Database Mysql Tutorial 考勤问题思路和解决

考勤问题思路和解决

Jun 07, 2016 pm 04:02 PM
host Ideas Attendance Attendance System solve question

最近在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退,目前的打卡控制器可以记录用户名和打卡时间,用户可能一天打卡多次,也可能一天只打了一次卡,这些情况都需要考虑。打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人

最近在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退,目前的打卡控制器可以记录用户名和打卡时间,用户可能一天打卡多次,也可能一天只打了一次卡,这些情况都需要考虑。打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人员,并且能显示缺勤、迟到和早退的时间。

考勤表

1

2

3

4

5

6

CREATE TABLE [dbo].[kaoqin](

    [user_name] [varchar](50) NULL,

    [card_time] [datetime] NULL

) ON [PRIMARY]

 

GO

Copy after login
插入测试数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

INSERT INTO [master].[dbo].[kaoqin]

select '张三', '2014-08-03 09:36:00'

union all

select '张三', '2014-08-03 18:10:00'

union all

select '张三', '2014-08-04 08:32:00'

union all

select '张三', '2014-08-04 15:15:00'

union all

select '张三', '2014-08-05 09:32:00'

union all

select '张三', '2014-08-05 15:15:00'

union all

select '张三', '2014-08-01 08:36:00'

union all

select '张三', '2014-08-01 18:10:00'

union all

select '张三', '2014-08-02 08:32:00'

union all

select '张三', '2014-08-02 18:15:00'

union all

select '张三', '2014-08-25 08:00:00'

union all

select '张三', '2014-08-24 19:00:00'

union all

select '张三', '2014-08-27 08:00:00'

union all

select '张三', '2014-08-27 17:00:00'

union all

select '张三', '2014-08-26 10:00:00'

union all

select '张三', '2014-08-26 18:30:00'

union all

select '张三', '2014-08-26 8:00:00'

union all

select '张三', '2014-08-27 18:56:00'

           

GO

Copy after login
我的思路是用一张临时表得到这个月的所有工作日,将该临时表与用户进行交叉连接,这样每个用户在这个月的每个工作日都有一条记录。假设早上9点为上班时间,18点为下班时间,这个可以后续做成变量的形式。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

declare @time_start datetime

declare @time_end datetime

 

set @time_start = '2014-08-01 00:00:00'

set @time_end = DATEADD(M,1,@time_start)

 

-- 一个月的工作日

IF object_id('tempdb..#tempDate') is not null

BEGIN

    drop table #tempDate

END

CREATE table #tempDate

(  

    stat_day varchar(10)

)

 

IF object_id('tempdb..#tempUserDate') is not null

BEGIN

    drop table #tempUserDate

END

CREATE table #tempUserDate

(  

     

    stat_day varchar(10),

    [user_name] varchar(40)

)

CREATE clustered index tempUserDate_Index1 on #tempUserDate ([user_name],stat_day)

 

declare @time_temp datetime

set @time_temp = @time_start

while @time_temp < @time_end

begin

   if datepart(weekday,@time_temp)>1 and datepart(weekday,@time_temp)<7

   begin

       insert into #tempDate (stat_day) values (CONVERT(varchar(10),@time_temp,121))

   end

   set @time_temp= dateadd(d,1,@time_temp)

end

 

insert into #tempUserDate

select * from #tempDate  cross join

(select distinct [user_name] from [kaoqin]) t

Copy after login
从原始的kaoqin表中查询出每个用户的上班时间和下班时间,如果用户一天的打开记录超过两条,那么就会取最早和最晚的一条分别作为上班时间和下班时间。

1

2

3

select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,

    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]

    group by [user_name],CONVERT(varchar(10),card_time,121)

Copy after login
通过临时表#tempUserDate和上面的查询结果关联,如果左联接为空,则证明该人员缺勤。

1

2

3

4

5

6

7

8

9

--缺勤

select * from #tempUserDate a

left join

(

    select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,

    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]

    group by [user_name],CONVERT(varchar(10),card_time,121)

) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_day

where [b].[user_name] is null

Copy after login
下面是迟到和早退的实现SQL。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

--迟到

select * from #tempUserDate a

left join

(

    select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,

    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]

    group by [user_name],CONVERT(varchar(10),card_time,121)

) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_day

where CONVERT(varchar(100), [b].[on_time], 8)>&#39;09:00:00&#39;

 

--早退

select * from #tempUserDate a

left join

(

    select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,

    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]

    group by [user_name],CONVERT(varchar(10),card_time,121)

) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_day

where CONVERT(varchar(100), [b].[off_time], 8)<&#39;18:00:00&#39;

Copy after login
得到的结果

\

如果某个人他今天既迟到又早退在最终的结果中都会体现,可以从2014-08-05这条数据看出。当然,这个考勤系统还不完善,例如没有将节日考虑进来,初步的考虑是采用Job定期存储每年的节日,如果员工请假,也需要纳入到系统的考虑中。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Solution to the problem that Win11 system cannot install Chinese language pack Solution to the problem that Win11 system cannot install Chinese language pack Mar 09, 2024 am 09:48 AM

Solution to the problem that Win11 system cannot install Chinese language pack With the launch of Windows 11 system, many users began to upgrade their operating system to experience new functions and interfaces. However, some users found that they were unable to install the Chinese language pack after upgrading, which troubled their experience. In this article, we will discuss the reasons why Win11 system cannot install the Chinese language pack and provide some solutions to help users solve this problem. Cause Analysis First, let us analyze the inability of Win11 system to

Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

As smartphone technology continues to develop, mobile phones play an increasingly important role in our daily lives. As a flagship phone focusing on gaming performance, the Black Shark phone is highly favored by players. However, sometimes we also face the situation that the Black Shark phone cannot be turned on. At this time, we need to take some measures to solve this problem. Next, let us share five tips to teach you how to solve the problem of Black Shark phone not turning on: Step 1: Check the battery power. First, make sure your Black Shark phone has enough power. It may be because the phone battery is exhausted

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the &quot;My&quot; button in the lower right corner; (2) On the personal center page, find &quot;Settings&quot; and click it; (3) Scroll down and find the &quot;Clear Cache&quot; option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

Everyone knows that if the computer cannot load the driver, the device may not work properly or interact with the computer correctly. So how do we solve the problem when a prompt box pops up on the computer that the driver cannot be loaded on this device? The editor below will teach you two ways to easily solve the problem. Unable to load the driver on this device Solution 1. Search for "Kernel Isolation" in the Start menu. 2. Turn off Memory Integrity, and it will prompt "Memory Integrity has been turned off. Your device may be vulnerable." Click behind to ignore it, and it will not affect the use. 3. The problem can be solved after restarting the machine.

Interpreting Oracle error 3114: causes and solutions Interpreting Oracle error 3114: causes and solutions Mar 08, 2024 pm 03:42 PM

Title: Analysis of Oracle Error 3114: Causes and Solutions When using Oracle database, you often encounter various error codes, among which error 3114 is a relatively common one. This error generally involves database link problems, which may cause exceptions when accessing the database. This article will interpret Oracle error 3114, discuss its causes, and give specific methods to solve the error and related code examples. 1. Definition of error 3114 Oracle error 3114 pass

How to solve the problem that Huawei browser has stopped accessing this webpage How to solve the problem that Huawei browser has stopped accessing this webpage Feb 26, 2024 pm 01:28 PM

How to solve the problem that Huawei browser has stopped accessing this webpage? When using Huawei mobile browser to access certain websites, a prompt indicating that access is prohibited may appear, preventing users from browsing related content normally. This is very inconvenient for users. So, what should we do when we encounter a situation where access to the Huawei mobile browser website is prohibited? The editor below will provide you with solutions to the problem of prohibiting access to Huawei browser websites. I hope it will be helpful to you. Solution to the prohibition of access to the Huawei Browser website 1. After opening the Huawei mobile browser, click the three-dot icon below, and then click Settings. 2. After entering the settings, click [Security and Privacy] 3. Turn off the switch on the right side of [Safe Browsing] to remove website access restrictions. The above is the solution to the ban on Huawei browser website access.

Black Shark mobile phone charging troubleshooting and solutions Black Shark mobile phone charging troubleshooting and solutions Mar 22, 2024 pm 09:03 PM

Black Shark is a smartphone brand known for its powerful performance and excellent gaming experience. It is loved by gamers and technology enthusiasts. However, just like other smartphones, Black Shark phones will have various problems, among which charging failure is a common one. Charging failure will not only affect the normal use of the mobile phone, but may also cause more serious problems, so it is very important to solve the charging problem in time. This article will start with the common causes of Black Shark mobile phone charging failures and introduce methods to troubleshoot and solve charging problems. I hope it can help readers solve the problem of Black Shark mobile phones.

Are you worried about WordPress backend garbled code? Try these solutions Are you worried about WordPress backend garbled code? Try these solutions Mar 05, 2024 pm 09:27 PM

Are you worried about WordPress backend garbled code? Try these solutions, specific code examples are required. With the widespread application of WordPress in website construction, many users may encounter the problem of garbled code in the WordPress backend. This kind of problem will cause the background management interface to display garbled characters, causing great trouble to users. This article will introduce some common solutions to help users solve the trouble of garbled characters in the WordPress backend. Modify the wp-config.php file and open wp-config.

See all articles