我正在尝试在某些日期执行减法运算,在我的特定情况下,我只需要减去工作日(不包括周末,即周日和周六)
我的桌子是这样的:

date         | days_to_subtract   | dayName
23-04-2004   |   3                | Friday
23-05-2004   |   5                | Monday
21-04-2004   |   7                | Tuesday
25-04-2004   |   30               | Monday
01-04-2004   |   22               | Thursday

我的目标是计算一个新的列(新日期),从日期减去天数,只考虑工作(劳动)日,不包括(或忽略)周末日。
我试过这样的方法:
SELECT
    CASE WHEN dayName = "Monday" then date_sub(date, days_to_subtract + 2)
         WHEN dayName = "Tuesday" then date_sub(date, days_to_subtract + 1)
         ELSE date_sub(date, days_to_subtract)
    END AS newColumn
FROM
    "MyTable"

但当减天数大于5时,这将不起作用。
有没有建议使用简单的SQL和不复杂的函数来解决这个问题?

最佳答案

我们先来解释一下你的情况:
星期一)Suposse我们在星期一定位,那么当减去N(虚拟)天时,您期望(实际)减去的结果如下:

"Mon" - 1 day  => "Mon" - (1 + 2 * (1)) days => "Mon" - 3 days  /* 1 weekend affecting */
"Mon" - 2 days => "Mon" - (2 + 2 * (1)) days => "Mon" - 4 days  /* 1 weekend affecting */
"Mon" - 3 days => "Mon" - (3 + 2 * (1)) days => "Mon" - 5 days  /* 1 weekend affecting */
...
"Mon" - 6 days => "Mon" - (6 + 2 * (2)) days => "Mon" - 10 days /* 2 weekends affecting */
...

星期二)Suposse我们定位在星期二,那么当减去N(虚拟)天时,您期望(实际)减去的结果如下:
"Tue" - 1 day  => "Tue" - (1 + 2 * (0)) days => "Tue" - 1 days  /* 0 weekends affecting */
"Tue" - 2 days => "Tue" - (2 + 2 * (1)) days => "Tue" - 4 days  /* 1 weekend affecting */
"Tue" - 3 days => "Tue" - (3 + 2 * (1)) days => "Tue" - 5 days  /* 1 weekend affecting */
...
"Tue" - 6 days => "Tue" - (6 + 2 * (1)) days => "Tue" - 8 days  /* 1 weekend affecting */
"Tue" - 7 days => "Tue" - (7 + 2 * (2)) days => "Tue" - 11 days /* 2 weekends affecting */
...

星期三)Suposse我们定位在星期三,那么当减去N(虚拟)天时,您希望(实际)减去的结果如下:
"Wed" - 1 day  => "Wed" - (1 + 2 * (0)) days => "Wed" - 1 days  /* 0 weekends affecting */
"Wed" - 2 days => "Wed" - (2 + 2 * (0)) days => "Wed" - 2 days  /* 0 weekends affecting */
"Wed" - 3 days => "Wed" - (3 + 2 * (1)) days => "Wed" - 5 days  /* 1 weekend affecting */
...
"Wed" - 6 days => "Wed" - (6 + 2 * (1)) days => "Wed" - 7 days  /* 1 weekend affecting */
"Wed" - 7 days => "Wed" - (7 + 2 * (1)) days => "Wed" - 9 days  /* 1 weekend affecting */
"Wed" - 8 days => "Wed" - (8 + 2 * (2)) days => "Wed" - 12 days /* 2 weekends affecting */
...

我们可以注意到,这个逻辑中间有一个数学演算。如果我们以这种方式索引一周中的几天:
0 <-> Monday
1 <-> Tuesday
2 <-> Wednesday
3 <-> Thursday
4 <-> Friday

那么,一个与之前的微积分相匹配的公式是:
Week_Day - X days => Week_Day - X days + 2 * CEIL((X - INDEX_OF(Week_Day)) / 5) days

where INDEX_OF() is a function that returns the previous indexing for days.

例1)
假设我们需要从星期三减去两天,那么前面的公式将缩减为:
"Wed" - 2 days => "Wed" - 2 + 2 * CEIL((2 - 2) / 5)
"Wed" - 2 days => "Wed" - 2 + 2 * CEIL(0 / 5)
"Wed" - 2 days => "Wed" - 2 + 2 * 0
"Wed" - 2 days => "Wed" - 2 + 0
"Wed" - 2 days => "Wed" - 2 days

例2)
假设我们需要从星期三减去8天(在这种情况下,我们有两个周末在减法的中间),那么前面的公式将减少为:
"Wed" - 8 days => "Wed" - 8 + 2 * CEIL((8 - 2) / 5)
"Wed" - 8 days => "Wed" - 8 + 2 * CEIL(6 / 5)
"Wed" - 8 days => "Wed" - 8 + 2 * 2 /* Two weekends are affecting */
"Wed" - 8 days => "Wed" - 8 + 4
"Wed" - 8 days => "Wed" - 12 days

因此,基于这个公式,并操作MySQL DAYOFWEEK()方法来生成INDEX_OF()函数,我们可以执行下一个查询:
SELECT
    date AS OriginalDate,
    DAYOFWEEK(date) - 2 AS IndexOfDay,
    days_to_subtract AS DaysToSubtract,
    2 * CEIL((days_to_subtract - (DAYOFWEEK(date) - 2)) / 5) AS WeekendDaysToAdd,
    DATE_SUB(
        date,
        INTERVAL days_to_subtract + (2 * CEIL((days_to_subtract -  (DAYOFWEEK(date) - 2)) / 5)) DAY
    ) AS CalucaltedDay
FROM
    myTable

最后一列calculateDay将包含您希望从减法中获得的日期,其他列仅用于检查数学演算的步骤。我希望你能理解这背后的逻辑,因为我花了一些时间去弄清楚并试图解释它。您可以看到下一个链接的工作示例:
1)DB Fiddle
2)SQL Fiddle

关于mysql - 从忽略周末的日期开始减去几天,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53015876/

10-13 04:52