patternsqlMinor
Getting the number of Saturdays and Sundays in a month
Viewed 0 times
sundaysnumberthegettingmonthandsaturdays
Problem
I am trying to get the number of Saturdays and Sundays for a given month represented by an end_date and a start date.
I have been trying to understand the algorithm in the SO question here . I have understood the matrix they have created but I don't get the algorithm that is being used, plus the thing is just the opposite of what is needed, and this thing is pretty new to me, mostly I do normal
It would be helpful to get some ideas on this issue.
I have been trying to understand the algorithm in the SO question here . I have understood the matrix they have created but I don't get the algorithm that is being used, plus the thing is just the opposite of what is needed, and this thing is pretty new to me, mostly I do normal
select, insert, update and joins in mysql.It would be helpful to get some ideas on this issue.
Solution
I'll suggest the calendar table also discussed in that thread. It will help for a lot of calculations beside this one. Your query can then be expressed as:
or as:
select count( case when DAYOFWEEK(calendar_date) in (1,7) then 1 end)
from Calendar
where calendar_date between :start_date and :stop_dateor as:
select count(1)
from Calendar
where calendar_date between :start_date and :stop_date
and DAYOFWEEK(calendar_date) in (1,7)Code Snippets
select count( case when DAYOFWEEK(calendar_date) in (1,7) then 1 end)
from Calendar
where calendar_date between :start_date and :stop_dateselect count(1)
from Calendar
where calendar_date between :start_date and :stop_date
and DAYOFWEEK(calendar_date) in (1,7)Context
StackExchange Database Administrators Q#87146, answer score: 8
Revisions (0)
No revisions yet.