Calculate Average per Day MYSQL - mysql

I am running MYSQL version 4.0.16. My hosting provider manages the server and therefore I cannot upgrade to a later version.
I have been given a task of building a help desk report. It needs to show the total number of calls over an 8 day period per day , the total number of calls closed on the same day and it then needs to show the average number of calls for each of these days over an 30 day period.
For formatting purposes I have had to left join a list of dates. This is purely so my report will not have any missing dates.
I am struggling to figure out how I can calculate the average number of calls per date over a 30 days period. Can anyone help me do this? The average should be calculated per day, so the function needs to count all of the calls over the last 30 days prior to each date and then divide this by 30 to calculate the average.

SELECT COUNT(call_id)/30
FROM call_table
WHERE call_date BETWEEN curdate() and adddate(curdate() interval -30 day)
Not tested. I normally use SQL server so the MySQL syntax may be slightly off, but if I am understanding you correctly then it would be similar to this.

Related

MYSQL query that works out the total amount of days between different time periods for a payroll

I know this question may seem like a simple one and might have been asked before but what makes this tricky is how do I work out the total days for a payroll. Now my company's drivers work on how many days they are clocked onto a truck. I need to calculate the total amount of days they have been clocked in per month.
The payroll dates are the 25th of the previous month and the 26th of the current month. If a driver is clocked in before the 26th of the payroll month the amount of days still need to be calculated up until the 26th. Similarly if the driver was clocked in before the 25th and only clocks out during the payroll month only the days between the 25th and the clocked out date needs to be calculated.
I have a php solution to this but it created so many over heads and is not optimized at all, it also creates loading problems and the way the calculations are done is based of the amount clock outs and manually adding or subtracting days according to certain conditions. This is not a a very good way of calculating the amount of clocked in days per driver as you can see so I am trying to make a query that will handle the calculation of the days for the payroll.
the problem I have with this current query is that it seems not able to see that the date 2022-04-06 20:42 is smaller than 2022-04-25 07:59
provided below is the query and the data it's returning.... Just excuse the query it does seem a bit hard to follow as I still need learn how to create MySQL variables so there's a lot of repetition. (The STR_TO_DATE() is becasue the dates are being saved as type strings in the data base)
MySQL query:
SELECT
IF(STR_TO_DATE(OPENING_DATE, '%Y-%m-%d %H:%i') < DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y-%m-26 00:01'),
DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y-%m-26 00:01'),OPENING_DATE) AS OPENING_DATE,
IF(STR_TO_DATE(CLOSING_DATE, '%Y-%m-%d %H:%i') = '',
DATE_FORMAT(CURRENT_DATE(), '%Y-%m-25 23:59'), CLOSING_DATE) AS CLOSING_DATE,
TIME_TO_SEC(TIMESTAMPDIFF( MINUTE,
IF(STR_TO_DATE(OPENING_DATE, '%Y-%m-%d %H:%i') = '' ,
DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y-%m-26 00:01'),OPENING_DATE),
IF(STR_TO_DATE(CLOSING_DATE, '%Y-%m-%d %H:%i') < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-25 23:59'),
DATE_FORMAT(CURRENT_DATE(), '%Y-%m-25 23:59'), CLOSING_DATE)
))/3600 AS TIME_DIFF_01
FROM HANDOVERS
WHERE (DRIVER_ID = '8308215990085');
Returned Data:
For reference this is the current data saved in the data base. Here you can compare the saved clocked in and out data to the data I'm trying to pull. I honestly don't know why the second batch of data has different days to the first.
How about this...
When you insert a row, check to see if it spans the month boundary (morning of the 26th). If it does, split it into two (or more) records.
Add an extra column that says which month the row belongs to.
With those, the Select becomes much simpler.

How many Intervals are in a period?

Given a table with two dates and a date interval (i.e. 6 WEEKS), how can I count how many times this interval fits in this date interval?
In my PHP script I'm using a simple for loop, but I'd like to add this in a MySQL query.
A simple calculation would be to count the number of days between the dates and then assume a month has 30 days and a year has 365 days, but that's not always true.
How can I calculate with datediff and a date interval string?
When I understood you correctly, you need the timestampdiff() function.
The first parameter is the unit of the interval. The result is how many units are between the two dates.

MySQL Finding Number of Elapsed Weeks in Current Month

QUESTION
Is it possible the find the number of weeks in the current month using a MySQL query?
If you answer the above question, then you have answered my question. However, I have been downvoted before for not including context, so....
BACKGROUND (Warning: It Will Confuse You)
I advise not reading any further
Because of a MySQL parser we have written in an application, my hands are tied with the way in which I must approach writing queries.
Here's the scenario:
I need to average out the number of hours worked per week over the course of a month by an employee; however, I must approach this by generating a column returned by the query that can then be summed to give me the average (yes, that is a mouth full).
Here is what I mean (and one attempt):
SELECT ets.*, (ets.hours/4) AS AVG_HOURS
FROM employee_timesheet AS ets
WHERE ets.date_worked >= (NOW() - INTERVAL 1 MONTH)
Yes, the WHERE clause is not accurate, that will have to change, too (but that is the next step).
The application then SUMS(avg_hours). Consider that each ets record contains the hours worked per day, by dividing the (hours worked each day) / by the (floating representation of the number of weeks in the month), I should then get the average worked per week over the course of the month by summing the result of the division.
To represent the logic in pseudo-code:
SUM(days_hours/number_of_weeks)
You can use DATEDIFF function
SELECT DATEDIFF('2014-10-30','2014-12-29') AS DiffDate
it should return the days between two date
https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
To answer the question, yes you can find the number of weeks from a month by using SQL's
datepart(week, date).
If you need the current month's number of weeks you can do
Datepart(week, getdate())
Here's more information for DATEPART
To get the first day of the month you can use
DATEADD(month, DATEDIFF(month, 0, #mydate), 0)
Alright, I got it. If I want to get the current number of weeks elapsed in the current month, I can:
DAYOFMONTH(CURRENTDATE()) / 7

Selecting PHP data by date for the last 7 days from mysql database with a different date layout

I have a transactions table in my database where the date is stored in the date field like this: 2014-08-30 02:22:35.
I'm making basic analytics and need to be able to display all transactions for each day for the last 7 days but am a little confused as to how I can achieve this when there is a timestamp along with the date stored in the same field.
Any suggestions would be greatly appreciated.
You can get the last seven days with:
where `date` >= CURDATE() - interval 7 day
This will go back seven days, ignoring the time field.
I'm not sure what you mean by "display all transactions for each day for the past 7 days". You can extract just the date for the field using date (so, date(date)) and use the value for filtering, aggregation, or sorting.

PHP / MYSQL Date Calculations for automated billing

I am sending 'reminder' messages every 30 days, and was wondering how to calculate the time based on the timestamp I placed on the originating users signup date.
I will have a cron job run every day to query and send the messages, but I am unsure how to query the date so it will select the appropriate accounts each day and send the messages.
Any sure fire ways to do this?
Use the DATEDIFF function to return the difference between then and NOW() in days.
Something like the query below should fit. If you can give me any more details on what / how you wish to query I can make this SQL more specific. This current query selects users whose signup_date was 30 or more days ago:
SELECT * FROM users WHERE DATE_ADD(signup_date, INTERVAL 30 DAY) <= NOW()
For further information:
MySQL Date and Time Functions
Some notes on INTERVAL usage