How to get the number of weekend days in mysql? - mysql

I want to count the number of saturdays and sundays in a certain year. Is there any way that I can do that in sql?

As alternative solution you could have a look at the answer in this post right here.
It shows how to calculate the weekdays between to dates and can be easily adjusted to calculate Saturdays/Sundays.

Related

Trying to calculate the number of working days from a given date and to exclude weekends in MYSQL

I'm trying to select a column that would reference a date-time value and allow me to find the date given an addition or subtraction of weekdays from the referenced date. I know I can calculate the number of days between with DateDiff but that hasn't seemed as useful in trying to determine a landing date X number of days from a given reference. Is there a way of leveraging DATE_ADD or another function for this?

Laravel array filtering

Developers, I am working on a report that I have to display the companies ranking based on some conditions for the date range which comes from front. Ex: last week, I have done that, but now I want to show how many weeks that a particular company be in same rank. If I am checking the past week I have to check the ranking for each week from the year start. If last week first position company and other weeks first position company is same I have make the count as increasing accordingly. When I querying the data for each week using the for loop it is taking around 42s to process and display the data. Also I tried to fetch whole data from first week of the year to current week then I filtered the array but this also takes long time. Can anyone give any other ideas to overcome this? Thanks in advance.
As far as I understand your problem - then storage of aggregated data should help you.
Create a table in the database, let's say "archive_rating", with 3 fields: week_number (let from January 1, 2000), company_id, company_position in your rating. Don't forget the index for the week_number field.
At 00:00:00 every Monday, run a background task that will save the positions for each company to an archive table.
This will allow you not to calculate a rating for each week from the beginning of the year. You will already have it.

Avg difference between consecutive mysql rows for specific month

I'm busy creating a personal mobile web app for home management.
Each day a READING is entered into a table, along with the days DATE.
One reading per day, meaning DATE is unique. READING could be the same, by very unlikely, if there is no usage for that day.
A usage amount for the day is calculated, by subtracting the previous days reading from the newly input reading.
How would I calculate the average usage numbers for a particular month?
Should the usage amount, once calculated, perhaps be stored back to the newly added row? Leaving for easy use of this to find an average?
Should a separate primary key be added, numbering the records, as apposed to using date to calculate the latest record added?
Thank you in advance, any help appreciated
Final solution:
When adding the latest readings, calculate day usage using previous record (found by using date). Add this to a third column.
To find daily average for current month, use MySQL avg, and limit to current year, and current month.

Calculating a day on a specific date

Is there any direct way to know the day on a specific date?
For example, take any date say 23-08-1980. Now, I want to know the day on this date (It could be any starting from Monday to Sunday).
Is there any formula or any other way to calculate this statistically?
Do not use any programming language.
You need some meta information about the year for example the first Sunday of every month in every year, from there on you should be able to calculate the day faster.
http://www.jimloy.com/math/day-week.htm
Have a look at this Wikipedia page, http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week#A_tabular_method_to_calculate_the_day_of_the_week
It goes over leap years as well.

How to find in MySQL the nearest (time wise) entry to a given date when only given year or only year and month?

This is very similar to a question that has already been answered (that I can't re-find right now) but the answers only let you get the nearest entry when you have a full date (year, month and day).
I'd go with DATEDIFF and construct a date string assuming '01' as the day / month. Not nice, but should work though.
A simple ABS(DATEDIFF()) might be exactly what's needed. But, for example, given the reference date of '2009', is 2009/01/10 or 2008/12/31 the correct date to return? Depending on your requirement you might use the middle of the period, or end, rather than the start.
Another consideration is what to do if more than one date is equally distant from the reference.
Given '2009' as the reference date, which is closest: 2009/06/30 or 2009/07/01? One interpretation might be that both are the same 'distance' from 2009 - zero years. Do you need to have some rule for picking just one date (could be a simple as just taking the first date) or do you want all 'equally distant' dates reported.