get average hours in php language - mysql

I have a table with 3 columns:
id start_service stop_service
I have already managed to catch the time difference between start_service and stop_service using this query:
SELECT
TIMEDIFF (stop_service, start_service) AS tempo
FROM
user_establishment ORDER BY id;
Now I need to add all the results of this query and divide by the number of records, so as to obtain the average time of all services.
The main problem is the conversion of the hours.
Can someone please help me?

Try this if you need to get your results in hours. You can omit the division by 3600 if you need to have it in seconds (this is what I would do and then manipulate it in my code afterwards).
SELECT
AVG(TIME_TO_SEC(TIMEDIFF(stop_service, start_service)))/3600 AS tempo
FROM
user_establishment;
Hope this helps!

If you want the average time difference in hours upto two decimal points, you can try the below query.
select ROUND(AVG(TIME_TO_SEC(tempo)/3600), 2) as avg_time
from (
select TIMEDIFF(stop_service, start_service) as tempo
from user_establishment
) as timeline
You can modify the average time diff as per your need.

Following query will be used to get average of time difference.
No need to add ORDER BY clause while using AVG() function in mysql query. It saves time to being execute.
SELECT AVG(TIMEDIFF(stop_service, start_service)) AS tempo
FROM user_establishment;

Related

What is the default time-duration when using aggregation functions?

Newbie here!
I was running a query where I used timediff to figure out the duration of two trips.
I made a CTE for the same.
I wanted to get the average duration grouped by membership plans (there are 2 scenarios - 1)member 2)non-member[casual])
Here is the code that I wrote
WITH CTE as (SELECT member_casual, timediff(ended_at, started_at) as duration, datediff(ended_at, started_at) as dateduration
FROM prayag.combined_data)
SELECT Avg(duration), member_casual
FROM CTE
GROUP BY member_casual
[Image for reference][1]
As you can see I got the average of duration with this code, but the format is not specified. Is this data in hours, minutes, or seconds? How can I modify this code so that I can change the avg duration in the (dd):hh:mm:ss format?
PS: Pointing out any other mistakes is highly appreciated
[1]: https://i.stack.imgur.com/CjLHY.png

SQL concat two fileds into time to calculate TIMESTAMPDIFF

So I have the following table
Agenda
-hour_begin
-minute_begin
-hour_end
-minute_end
Note: All these fields are varchar(2).
I need to calculate the difference in minutes between (hour_begin/minute_begin) and (hour_end/minute_end)
This is what I have tried so far, but without success.
SELECT TIMESTAMPDIFF(MINUTE,CONCAT(hour_begin.''.minute_begin),CONCAT(hour_end.''.minute_end)) AS diff
This keeps returning an error.
Thanks in advance!
You can calculate the number of hours * 60, then add the number of minutes.
Something like this:
select (60*(hour_end - hour_begin)) +
(minute_end - minute_begin)
from your_table

phpMyAdmin - Calculate daily average

I have a database which shows the temperature in a certain location over time. The values are measured every 10 minutes as shown in the picture. Every location has an own ID called "Stationsnummer" (Station number).
What i want to do is calculate daily averages. So i need to write a query which calculates a daily average of the column "Temperatur Oberfläche".
With the query:
SELECT AVG (`Temperatur Oberfläche [°C]`)
FROM `temperatur oberfläche`
WHERE `Stationsnummer` LIKE '4900180611' AND `Datum` like '1998-11-10'
I get the average of one day. But in the end I wanna have something like this as a result:
Does someone has an idea how it can work?
Thanks a lot!
You can use GROUP BY for this:
select `datum`, avg(`Temperatur Oberfläche [°C]`)
from `temperatur oberfläche`
WHERE `Stationsnummer` = '4900180611'
group by `datum`;
Further if you want to find avg temp for all the stationsnummer for each day, you can include that too, like this:
select `Stationsnummer`, `datum`, avg(`Temperatur Oberfläche [°C]`)
from `temperatur oberfläche`
group by `Stationsnummer`, `datum`;

Average between two columns in mysql that are date not timestamp

Alright so here it is. I need to figure out the average amount of days between two columns.
Column 1 is recieved_date and column 2 is fix_date
Just want to know how to take the two dates find the difference in days, do that for every row and pop out a number stating the average amount of days it takes to fix something.
Tried to find it online but every time I find something like it, they have two specific dates. I need the entire columns averaged.
You can use the TIMESTAMPDIFF function both for dates and datetime.
See Mysql average time between visits
Add a group by and some other columns to this and it should do the trick:
select
avg(fix_period)
from
(
select
datediff(fix_date, received_date) as fix_period
from some_table
) as a
;

Avg Time difference in mysql

Is there a function to find average time difference in the standard time format in my sql.
You can use timestampdiff to find the difference between two times.
I'm not sure what you mean by "average," though. Average across the table? Average across a row?
If it's the table or a subset of rows:
select
avg(timestampdiff(SECOND, startTimestamp, endTimestamp)) as avgdiff
from
table
The avg function works like any other aggregate function, and will respond to group by. For example:
select
col1,
avg(timestampdiff(SECOND, startTimestamp, endTimestamp)) as avgdiff
from
table
group by col1
That will give you the average differences for each distinct value of col1.
Hopefully this gets you pointed in the right direction!
What I like to do is a
SELECT count(*), AVG(TIME_TO_SEC(TIMEDIFF(end,start)))
FROM
table
Gives the number of rows as well...
In order to get actual averages in the standard time format from mysql I had to convert to seconds, average, and then convert back:
SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(timeA, timeB))))
If you don't convert to seconds, you get an odd decimal representation of the minutes that doesn't really make any sense (to me).
I was curious if AVG() was accurate or not, the way that COUNT() actually just approximates the value ("this value is an approximation"). After all, let's review the average formula: average = sum / count. So, knowing that the count is accurate is actually really important for this formula!
After testing multiple combinations, it definitely seems like AVG() works and is a great approach. You can calculate yourself to see if it's working with...
SELECT
COUNT(id) AS count,
AVG(TIMESTAMPDIFF(SECOND, OrigDateTime, LastDateTime)) AS avg_average,
SUM(TIMESTAMPDIFF(SECOND, OrigDateTime, LastDateTime)) / (select COUNT(id) FROM yourTable) as calculated_average,
AVG(TIME_TO_SEC(TIMEDIFF(LastDateTime,OrigDateTime))) as timediff_average,
SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(LastDateTime, OrigDateTime)))) as date_display
FROM yourTable
Sample Results:
count: 441000
avg_average: 5045436.4376
calculated_average: 5045436.4376
timediff_average: 5045436.4376
date_display: 1401:30:36
Seems to be pretty accurate!
This will return:
count: The count.
avg_average: The average based on AVG(). (Thanks to Eric for their answer on this!)
calculated_average: The average based on SUM()/COUNT().
timediff_avg: The average based on TIMEDIFF(). (Thanks to Andrew for their answer on this!)
date_display: A nicely-formatted display version. (Thanks to C S for their answer on this!)