MySQL函数查找两个日期之间的工作日数

MySQL函数查找两个日期之间的工作日数

Excel具有NETWORKDAYS()函数,可以查找两个日期之间的工作日数。

任何人都有类似MySQL的功能?由于假期增加了复杂性,解决方案不必处理假期。


隔江千里
浏览 2075回答 3
3回答

慕丝7291255

这个表达 -5 * (DATEDIFF(@E, @S) DIV 7) + MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY(@S) + WEEKDAY(@E) + 1, 1)计算开始日期@S和结束日期@E之间的工作日数。假设结束日期(@E)不在开始日期之前(@S)。与DATEDIFF兼容,因为相同的开始日期和结束日期为零工作日。忽略假期。数字串的构造如下。创建一个开始日和结束日的表,行必须以星期一(WEEKDAY 0)开头,列也必须以星期一开始。从左上角到右下角填写对角线,全部为0(即星期一到星期一,星期二和星期二之间有0个工作日,等等)。对于每天从对角线开始(必须始终为0)并填写右侧的列,一次一天。如果您在周末(非工作日)列登陆,则工作日数不会更改,而是从左侧开始。否则,工作日数会增加一个。当你到达行循环的末尾回到同一行的开头并继续直到你再次到达对角线。然后继续下一行。例如,假设周六和周日不是工作日 - | M T W T F S S-|--------------M| 0 1 2 3 4 4 4T| 4 0 1 2 3 3 3W| 3 4 0 1 2 2 2T| 2 3 4 0 1 1 1F| 1 2 3 4 0 0 0S| 1 2 3 4 5 0 0S| 1 2 3 4 5 5 0然后将表中的49个值连接到字符串中。如果您发现任何错误,请告诉我。-Edit改进表: | M T W T F S S-|--------------M| 0 1 2 3 4 4 4T| 4 0 1 2 3 3 3W| 3 4 0 1 2 2 2T| 2 3 4 0 1 1 1F| 1 2 3 4 0 0 0S| 0 1 2 3 4 0 0S| 0 1 2 3 4 4 0改进的字符串:'0123444401233334012222340111123400001234000123440'表达改善:5 * (DATEDIFF(@E, @S) DIV 7) + MID('0123444401233334012222340111123400001234000123440', 7 * WEEKDAY(@S) + WEEKDAY(@E) + 1, 1)

慕田峪9158850

该解决方案使用与Rodger基本相同的方法,除了生成矩阵的方法要复杂得多。注意:此解决方案的此输出与NETWORKDAYS不兼容。与Rodger的解决方案一样,这可以计算开始日期(@S)和结束日期(@E)之间的工作日数,而无需定义存储过程。它假定结束日期不在开始日期之前。使用相同的开始和结束日期将产生0.不考虑假期。这与Rodger的解决方案之间的主要区别在于矩阵和结果的数字串是由一个我没有包含的复杂算法构造的。该算法的输出由单元测试验证(参见下面的测试输入和输出)。在矩阵中,任何给定的x和y值对(WEEKDAY(@S)和WEEKDAY(@E)的交集产生两个值之间的工作日差异。分配顺序实际上并不重要,因为两者被加在一起绘制位置。营业日为周一至周五 | M T W T F S S-|--------------M| 0 1 2 3 4 5 5T| 5 0 1 2 3 4 4W| 4 5 0 1 2 3 3T| 3 4 5 0 1 2 2F| 2 3 4 5 0 1 1S| 0 1 2 3 4 0 0S| 0 1 2 3 4 5 0表中的49个值连接成以下字符串:0123455501234445012333450122234501101234000123450最后,正确的表达是:5 * (DATEDIFF(@E, @S) DIV 7) + MID('0123455501234445012333450122234501101234000123450', 7 * WEEKDAY(@S) + WEEKDAY(@E) + 1, 1)我已使用此解决方案验证了以下输入和输出:Sunday, 2012-08-26 -> Monday, 2012-08-27 = 0Sunday, 2012-08-26 -> Sunday, 2012-09-02 = 5Monday, 2012-08-27 -> Tuesday, 2012-08-28 = 1Monday, 2012-08-27 -> Monday, 2012-09-10 = 10Monday, 2012-08-27 -> Monday, 2012-09-17 = 15Monday, 2012-08-27 -> Tuesday, 2012-09-18 = 16Monday, 2012-08-27 -> Monday, 2012-09-24 = 20Monday, 2012-08-27 -> Monday, 2012-10-01 = 25Tuesday, 2012-08-28 -> Wednesday, 2012-08-29 = 1Wednesday, 2012-08-29 -> Thursday, 2012-08-30 = 1Thursday, 2012-08-30 -> Friday, 2012-08-31 = 1Friday, 2012-08-31 -> Saturday, 2012-09-01 = 1Saturday, 2012-09-01 -> Sunday, 2012-09-02 = 0Sunday, 2012-09-02 -> Monday, 2012-09-03 = 0Monday, 2012-09-03 -> Tuesday, 2012-09-04 = 1Tuesday, 2012-09-04 -> Wednesday, 2012-09-05 = 1Wednesday, 2012-09-05 -> Thursday, 2012-09-06 = 1Thursday, 2012-09-06 -> Friday, 2012-09-07 = 1Friday, 2012-09-07 -> Saturday, 2012-09-08 = 1Saturday, 2012-09-08 -> Sunday, 2012-09-09 = 0Monday, 2012-09-24 -> Sunday, 2012-10-07 = 10Saturday, 2012-08-25 -> Saturday, 2012-08-25 = 0Saturday, 2012-08-25 -> Sunday, 2012-08-26 = 0Saturday, 2012-08-25 -> Monday, 2012-08-27 = 0Saturday, 2012-08-25 -> Tuesday, 2012-08-28 = 1Saturday, 2012-08-25 -> Wednesday, 2012-08-29 = 2Saturday, 2012-08-25 -> Thursday, 2012-08-30 = 3Saturday, 2012-08-25 -> Friday, 2012-08-31 = 4Saturday, 2012-08-25 -> Sunday, 2012-09-02 = 0Monday, 2012-08-27 -> Monday, 2012-08-27 = 0Monday, 2012-08-27 -> Tuesday, 2012-08-28 = 1Monday, 2012-08-27 -> Wednesday, 2012-08-29 = 2Monday, 2012-08-27 -> Thursday, 2012-08-30 = 3Monday, 2012-08-27 -> Friday, 2012-08-31 = 4Monday, 2012-08-27 -> Saturday, 2012-09-01 = 5Monday, 2012-08-27 -> Sunday, 2012-09-02 = 5
打开App,查看更多内容
随时随地看视频慕课网APP