Home > front end >  How to calculate weekends of time range using MySQL
How to calculate weekends of time range using MySQL

Time:05-14

I need to calculate weekends of time range using MySQL.

This is what I have so far(about calculate working days but i want to calculate weekends):

select
    (floor(days / 7)* 5
      days%7
    - case
        when 6 between wd and wd   days%7 - 1 then 1
        else 0
    end
    - case
        when 7 between wd and wd   days%7 - 1 then 1
        else 0
    end) as result
from
    (
    select
        abs(datediff('2022-05-15', '2022-05-13'))   1 as days,
        weekday('2022-05-13')   1 as wd ) as a;

Hope I explained well...

Thanks.

CodePudding user response:

select
(floor(days / 7)* 2
  case
    when 6 between wd and wd   days%7 - 1 then 1
    else 0
end
  case
    when 7 between wd and wd   days%7 - 1 then 1
    else 0
end) as result 

from ( select abs(datediff('2022-05-15', '2022-05-13')) 1 as days, weekday('2022-05-13') 1 as wd ) as a;

Maybe this will work. Have a try.

  • Related