为女票写的计算工作时间的SQL
排除非工作时间、非工作日后,计算工作时间,代码如下:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | -- 删除函数 DROP FUNCTION IF EXISTS calculateWorkingTime; set @workStartTime= '09:30:00' ; set @workEndTime= '18:30:00' ; -- 创建函数 --/ CREATE FUNCTION calculateWorkingTime(startDate datetime,endDate datetime) RETURNS decimal (32,4) BEGIN DECLARE intnum int (255) DEFAULT 0; DECLARE decimalnum decimal (32,4) DEFAULT 0.000; DECLARE workStartTimeHour decimal (32,4) DEFAULT 0.000; DECLARE workEndTimeHour decimal (32,4) DEFAULT 0.000; DECLARE currentDay int (10) DEFAULT 0; DECLARE tempTimeHour decimal (32,4) DEFAULT 0.000; -- DECLARE temp varchar(2048) DEFAULT ''; -- deal starttime and endtime is nonworkdays SET startDate = ( CASE WHEN weekday(startDate)=5 THEN concat( date (timestampadd( day ,2,startDate)), ' ' ,@workStartTime) WHEN weekday(startDate)=6 THEN concat( date (timestampadd( day ,1,startDate)), ' ' ,@workStartTime) ELSE startDate END ); SET endDate = ( CASE WHEN weekday(endDate)=5 THEN concat( date (timestampadd( day ,-1,endDate)), ' ' ,@workEndTime) WHEN weekday(endDate)=6 THEN concat( date (timestampadd( day ,-2,endDate)), ' ' ,@workEndTime) ELSE endDate END ); -- SET temp = concat(temp,' ',startDate,';',endDate); if startDate < endDate then -- deal starttime and endtime is nonworktime if time (startDate)<=@workStartTime THEN SET startDate = concat( date (startDate), ' ' , @workStartTime); elseif date (startDate) < date (endDate) and time (startDate)>@workEndTime then SET startDate = concat( date (date_add(startDate, interval 1 day )), ' ' ,@workStartTime); end if; if time (endDate)>=@workEndTime then SET endDate = concat( date (endDate), ' ' ,@workEndTime); elseif date (startDate) < date (endDate) and time (endDate)<@workStartTime then SET endDate = concat( date (date_add(endDate, interval -1 day )), ' ' ,@workEndTime); end if; -- calculate time diff SET decimalnum = ( minute (endDate)*60+ second (endDate)- minute (startDate)*60- second (startDate))/3600; end if; -- calculate work time second SET workStartTimeHour = hour (@workStartTime)+ minute (@workStartTime)/60+ second (@workStartTime)/3600; SET workEndTimeHour = hour (@workEndTime)+ minute (@workEndTime)/60+ second (@workEndTime)/3600; -- WHILE (floor((unix_timestamp(endDate) - unix_timestamp(startDate))/3600) > 0) DO WHILE ((floor(unix_timestamp(endDate)/3600) - floor(unix_timestamp(startDate)/3600)) > 0) DO SET tempTimeHour = hour (startDate)+ minute (startDate)/60+ second (startDate)/3600; if workStartTimeHour <= tempTimeHour and tempTimeHour < workEndTimeHour then -- SET temp = concat(temp,' ',tempTimeHour,';'); SET intnum = ( CASE WHEN weekday(startDate)=5 or weekday(startDate)=6 then intnum ELSE intnum+1 END ); end if; SET startDate = timestampadd( hour ,1,startDate); END WHILE; SET decimalnum = intnum + decimalnum; -- concat(decimalnum,';',workStartTimeHour,' ',workEndTimeHour,' ',intnum,';;;',temp); RETURN decimalnum; END / -- select calculateWorkingTime('2017-02-17 07:30:00','2017-02-21 17:39:00'); select transport_id,create_at1,create_at2,create_at3, calculateWorkingTime(create_at1,create_at2), calculateWorkingTime(create_at2,create_at3),calculateWorkingTime(create_at1,create_at3) from newTable; select transport_id,calculateWorkingTime(create_at1,create_at2) from newTable; |
说明:第一次实现这种需求,当初写的时候又比较赶,写完后,发现,虽然功能实现了,但还有好多地方可以调优
女票是搞数据运营的,经常需要统计员工的工作时间;听女票说,这段SQL代码统计时好像有些问题,但还没来的急分析原因呢;女票又有新需求来了,不仅需要将非工作时间、非工作日去掉,还需要将节假日去掉 我将上面代码重构,实现想要功能(重构后的代码就不放出了)
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【刘超★ljc】。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
2016-05-29 统计电视机顶盒中无效用户数据,并以压缩格式输出有效用户数据