有考勤刷卡记录表,表名为attendance ,有如下字段:
姓名 卡号 刷卡时间 刷卡类型 name id time type   
张三 59775623 2010-04-01 07:23:37 null   
张三 59775623 2010-04-01 07:50:21 null   
张三 59775623 2010-04-01 18:20:22 null   
张三 59775623 2010-04-01 18:50:53 null   
李四 59775624 2010-04-01 07:00:06 null   
李四 59775624 2010-04-01 18:00:12 null   
李四 59775624 2010-04-02 08:20:32 null
李四 59775624 2010-04-02 17:00:22 null
李四 59775624 2010-04-01 18:00:08 null
.....................................................................................................
以下还有很多,每位员工每天都有,...............
现在需要更新刷卡的数据,需要对表attendance执行一个update动作,根据刷卡时间,需满足如下功能
 
1.如果刷卡时间是8:00以前,则type的值update之后就为“上班”;
 
2.如果刷卡时间是17:30以后,则type的值update之后就为“下班”;
 
3.如果刷卡时间为8:00~~12:00之间,则type的值update之后就为“迟到”;
 
4.如果刷卡时间为13:00~~17:30之间,则type的值update之后就为“早退”;
 
5.如果同一个人同一天在12:00以前有多次刷卡,则刷卡时间最早的那一笔记录其type值为“上班”,其余12:00以前的刷卡记录其type值update之后,变为“上班重复刷卡;
 
6. 如果同一个人同一天在13:00以后有多次刷卡,则刷卡时间最迟的那一笔记录其type值为“下班”,其余13:00以后的刷卡记录其type值update之后,变为“下班重复刷卡;
 
7.其余每天的任何时间段,update后,type值变为“乱刷卡”
 
小弟最头痛的是其中的5、6两种情况,可以使用where group by + haviing count(*)>1将其查出来,update就不知道如何处理了,小弟思考了好几天,也只能做到这一步,实在做不下去了,跑来求助各位达人;
 
问题补充:
1.请各位达人务必注意那个时间的格式,SQL里面转换时间格式可以使用convert(char(10),time,120),输出为YYYYMMDD;convert(char(8),time,112),输出格式为YYYYMMDD;convert(char(10),time,108),输出为HH-MM-SS
 
use test
go
if object_id('test.dbo.attendance'is not null drop table attendance 
-- 创建数据表
create table attendance 
(
name char(5),
id int,
time datetime,
type char(20)
)
go
--插入测试数据
insert into attendance select '张三',59775623,'2010-04-01 07:23:37',null
union all select '张三',59775623,'2010-04-01 07:50:21',null
union all select '张三',59775623,'2010-04-01 18:20:22',null
union all select '张三',59775623,'2010-04-01 18:50:53',null
union all select '李四',59775624,'2010-04-01 07:00:06',null
union all select '李四',59775624,'2010-04-01 18:00:12',null
union all select '李四',59775624,'2010-04-02 08:20:32',null
union all select '李四',59775624,'2010-04-02 17:00:22',null
union all select '李四',59775624,'2010-04-02 18:18:08',null
union all select '王五',59775625,'2010-04-01 08:02:06',null
union all select '王五',59775625,'2010-04-01 18:00:12',null
union all select '王五',59775625,'2010-04-02 07:20:32',null
union all select '王五',59775625,'2010-04-02 12:35:22',null
union all select '王五',59775625,'2010-04-02 18:18:08',null
go
 
*/
 
-->更新数据
update attendance set type=t2.type
from attendance t1
inner join
(
    select name,id,time=_time,type=case when time<='08:00' and idd=1 then '上班'
        when time>'08:00' and time<='12:00' and idd=1 then '迟到'
        when time<'12:00' and idd<>1 then '上班重复刷卡'
        when time>='13:00' and time<='17:30' and idd=1 then '早退'
        when time>'17:30' and idd=1 then '下班'
        when time>'13:00' and idd<>1 then '下班重复刷卡' 
        when time>='12:00' and time<='13:00' then '乱刷卡' end
    from
    (
        select name,id,_time=time,time=convert(varchar(5),time,8),type,
        idd=row_number()over(partition by convert(varchar(10),time,120),name order by time)
        from attendance where convert(varchar(5),time,8)<='12:00'
            union all
        select name,id,_time=time,time=convert(varchar(5),time,8),type,
        idd=row_number()over(partition by convert(varchar(10),time,120),name order by time)
        from attendance where convert(varchar(5),time,8)>='13:00'
            union all
        select name,id,_time=time,time=convert(varchar(5),time,8),type,idd=0
        from attendance where convert(varchar(5),time,8)>='12:00'
        and convert(varchar(5),time,8)<='13:00'
    )t
) t2
on t1.id=t2.id and t1.time=t2.time
 
-->显示更新后数据
select from attendance

/*测试结果
 
name id time type
--------------------------------------------------------------
张三     59775623    2010-04-01 07:23:37.000    上班                
张三     59775623    2010-04-01 07:50:21.000    上班重复刷卡        
张三     59775623    2010-04-01 18:20:22.000    下班                
张三     59775623    2010-04-01 18:50:53.000    下班重复刷卡        
李四     59775624    2010-04-01 07:00:06.000    上班                
李四     59775624    2010-04-01 18:00:12.000    下班                
李四     59775624    2010-04-02 08:20:32.000    迟到                
李四     59775624    2010-04-02 17:00:22.000    早退                
李四     59775624    2010-04-02 18:18:08.000    下班重复刷卡        
王五     59775625    2010-04-01 08:02:06.000    迟到                
王五     59775625    2010-04-01 18:00:12.000    下班                
王五     59775625    2010-04-02 07:20:32.000    上班                
王五     59775625    2010-04-02 12:35:22.000    乱刷卡              
王五     59775625    2010-04-02 18:18:08.000    下班                
 
(14 行受影响)
*/

mysql 考勤表异常 【待修改】的更多相关文章

  1. 阿里云ECS(linux)磁盘满触发的mysql的表异常修复案例

    阿里云ECS(linux)磁盘满触发的mysql的表异常修复案例 阿里云技术支持:完颜镇江 问题现象: 磁盘空间满了,第一想到的就是删除无用的服务日志或者升级数据盘. 通常是使用du –sh去分析目录 ...

  2. MySQL根据表前缀批量修改、删除表

    注意:请先调试好,以及做好备份,再执行操作. 批量修改表 批量给前缀为 xushanxiang_content_ 的表增加一个 username 的字段: SELECT CONCAT('ALTER T ...

  3. (转)mysql数据库表名批量修改大小写

    由于不用服务器对mysql的表名的大小写敏感要求不一致,经常在出现线上的数据库down到了本地不能运行的情况,贴出一段代码用来批量修改数据库表名大小写. DELIMITER // DROP PROCE ...

  4. mysql多表关联update修改操作

      UPDATE province_yunnan_salary s1 JOIN province_guangdong_salary s2 ON s1.user_name= s2.user_name S ...

  5. mysql数据库表的基本操作sql语句总结

    1,命令行登录命令 mysql -h localhost -u root -p C:\Users\lenovo>mysql -u root -p Enter password: ***** We ...

  6. 修改MYSQL数据库表的字符集

    MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...

  7. phpMyAdmin批量修改Mysql数据表前缀的方法

    多个网站共用一个Mysql数据库时,为使数据库管理不混乱,一般采用不同的网站使用不同前缀名的方式进行区分.而如何批量修改已有数据库的前缀名 呢?全部导出修改后再导入?还是一个表一个表的修改?今天我要介 ...

  8. 查看mysql数据库表大小和最后修改时间

    查看mysql数据库表相关信息如表大小.修改更新等信息,可以通过以下方式: 一   show table status like ’table_name‘ ; 二 在infortmation_sche ...

  9. MySQL使用pt-online-change-schema工具在线修改1.6亿级数据表结构

    摘  要:本文阐述了MySQL DDL 的问题现状.pt-online-schema-change的工作原理,并实际利用pt-online-schema-change工具在线修改生产环境下1.6亿级数 ...

随机推荐

  1. 【转载】 GNU GCC 选项说明

    GCC 1 Section: GNU Tools (1) Updated: 2003/12/05 Sponsor: GCC Casino Winning Content NAME gcc,g++-GN ...

  2. selenium firefox设置代理

    from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network. ...

  3. SuperMap iClient如何使用WMTS地图服务(转)

    http://blog.sina.com.cn/s/blog_6259ebd50102v221.html 什么是WMTS服务 WMTS,切片地图Web服务(Web Map Tile Service)当 ...

  4. Shell--变量内容的删除、替代与替换

    1. 变量内容的删除与替换 #代表由前面开始删除,所以这里便由开始的/删起,*来代替0到无穷多个任意字符 %由后面向前删除变量内容 例如:echo ${path%:*bin}删除最有一个目录,即从:到 ...

  5. k8s restful API 结构分析

    k8s的api-server组件负责提供restful api访问端点, 并且将数据持久化到etcd server中. 那么k8s是如何组织它的restful api的? 一, namespaced ...

  6. ECSHOP去版权(删除ECSHOP所有标识)

    前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop前者”ECSHOP演示站”在后台商店设置 – 商店标题修改后者” Powered by ecshop”打开 ...

  7. @property和@x.setter和@x.deleter表示可读可写可删除

    @property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的.1>只有@property表示只读.2>同时有@ ...

  8. es6 - filter for-chrome

    'use strict'; let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // 除去取余2的 - es6 let es5OddNumbers = numbers ...

  9. Android学习(十九)Dialog对话框

    一.什么是Dialog对话框 对话框是当前页面中弹出的一个小窗口,用于显示重要的提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等.用户需要与对话框进行交互,才能回到原窗 ...

  10. Jeewx 捷微管家操作配置文档(开源版本号)

    1.1.1.  公众帐号管理 (1)捷微是第三方微信公众帐号管理平台,使用本平台前,请自行注冊申请微信公众帐号,操作流程请參照百度经验[怎样注冊微信公众帐号]: http://jingyan.baid ...