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
Related
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;
I am having trouble understanding the structure of the query i wish to perform. What i have is a large set of data in a table with multiple UnitID's. The units have temperatures and Timestamps of when the temperatures where recorded.
I want to be able to display the data where I can see the Average temperature of each unit separated in a weekly interval.
Apologies for my previous post, I'm still a novice with querying. But i will show you what i have done so far.
SELECT UnitID AS 'Truck ID',
AVG(Temp) As 'AVG Temp',
LogTime AS 'Event Time',
DAY(g.`LogTime`) as 'Day',
MONTH(g.`LogTime`) as 'Month',
COUNT(*) AS 'Count'
FROM `temperature` as g
WHERE DATE_SUB(g.`LogTime`,INTERVAL 1 WEEK)
AND Ana > 13 AND Ana < 16 AND NOT g.Temp = -100
GROUP BY 'truck id', YEAR(g.`LogTime`),MONTH(g.`LogTime`),WEEK(g.`LogTime`)
Order BY 'truck id', YEAR(g.`LogTime`),MONTH(g.`LogTime`),WEEK(g.`LogTime`)
;
(Sorry, I don't know how to display a table result at the moment)
This result gives me the weekly temperature averages of a truck, and shows me on which day of the month the temperature was recorded, as well as a count of temperatures per week, per truck.
The Query I want to perform , creates 5 columns, being UnitID, Week1, Week2, Week3, Week4.
Within the 'Week' columns I want to be able to display a weekly(Every day of the Week) temperature average for each truck, where the following week is set a week after the previous week (ie. Week2 is set to display the avg(temp) one week from Week1).
And this is where I am stuck on the structure of how to create the query. Im not sure if i need to create sub-queries or use a Union clause. I have tried a couple of queries , but i have deleted them because they did not work. I'm not sure if this query is too complex or if its even possible.
If anyone will be able to help I would greatly appreciate it. If there is any other info I can supply that will help, I will try to do so.
Hopefully this is solvable. :p
MySQL has a WEEK function that will return the week of the year as an integer (0-52). You can use that in you GROUP BY clause, and then use the AVG aggregation function to get the average temperature. Your query would look something like this:
SELECT unitID, WEEK(dateColumn) AS week, AVG(tempColumn) AS averageTemperature
FROM myTable
GROUP BY unitID, WEEK(dateColumn);
Here is a list of other helpful Date and Time Functions that may be useful for querying your database.
I have a query that runs on our MYSQL database. It takes forever to run, so I would like to use BigQuery instead. The relevant table (a.xxx) is already in the cloud, and I've tried adjusting the code for BQ, but I'm not having any luck. The query basically pulls the number of individuals making purchases by day, and the number of those same individuals, who made another purchase 1-7 days after the initial purchase. I would appreciate any help!!!!
Here is the query:
select f.fts_date,
count(distinct f.FTS_id) as FTS_count,
count(distinct s.passportid) as SVS_count,
(count(distinct s.passportid)/count(distinct f.FTS_id)) as return_rate
from
(select passportid as FTS_id,addressid, date(signdatetime) as FTS_date from a.xxx
where date(signdatetime)>'2015-6-10' and fts="Y" and disposition="accepted") as f
left join a.xxx as s
on f.passportid=s.passportid and f.addressid=s.addressid and s.disposition="accepted" and
s.signdatetime between date_add(f.signdatetime, 1, "DAY") and date_add(f.signdatetime, 7, "DAY")
group by 1
BigQuery doesn't support INTERVAL keyword in DATE_ADD function, instead you should write it as
date_add(FTS_date, 1, "DAY")
See https://cloud.google.com/bigquery/query-reference#datetimefunctions for more details
I have 4 tables for different dates, the tables looks like this:
what I'm trying to do is to find the maximum tps for each service_name,function_name among all four days according to hour. for example in the figure I posted there is service_name(BatchItemService) in first raw that have (getItemAvailability) as function_name in date 13-06-12 01. I have same service_name for same function_name in all the other 3 tables for the same hour "01" but with different days, like day 13,14,15. I want to find maximum tps for this service_name,function_name set for hour "01" among all the four days.
I tried this, but it give me incorrect result.
SELECT
t.service_name,
t.function_name,
t.date,
max(t.tps)
FROM
(SELECT
service_name, function_name, date, tps
FROM
trans_per_hr_2013_06_12
UNION ALL
SELECT
service_name, function_name, date,tps
FROM
trans_per_hr_2013_06_13
GROUP BY service_name,function_name,date
UNION ALL
SELECT
service_name, function_name,date, tps
FROM
trans_per_hr_2013_06_14
UNION ALL
SELECT
service_name, function_name, date, tps
FROM
trans_per_hr_2013_06_15
UNION ALL
SELECT
service_name, function_name,date, tps
FROM
trans_per_hr_2013_06_16
) t
GROUP BY t.service_name,t.function_name,hour(t.Date);
Thanks a lot...
Your query looks like it should be returning what you want.
One possible issue is the type of the date column. As shown in the output, this looks like it might be stored as a character string rather than a date. If so, the following would work for the group by statement (assuming the format is as shown: DD-MM-YY H).
GROUP BY t.service_name,t.function_name, right(t.Date, 2);
As Bohemian says in the comment, this is not a good data structure. You have parallel tables and you are storing the date both in the table name and in a column. You should learn about table partitioning. This is a way that you can store different days in different files, but still have MySQL interpret them as one table. It would probably greatly simplify your using this data.
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!)