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`;
Related
I'm pretty new to SQL and I'm struggling with one of the questions on my exercise. How would I calculate average session length per daily active user? The table shown is just a sample of what the extended table is. Imagine loads more rows.
I simply used this query to calculate the daily active users:
SELECT COUNT (DISTINCT user_id)
FROM table1
and welcome to StackOverflow!
now, your question:
How would I calculate average session length per daily active user?
you already have the session time, and using AVG function you will get a simple average for all
select AVG(session_length_seconds) avg from table_1
but you want per day... so you need to think as group by day, so how do you get the day? you have a activity_date as a Date entry, it's easy to extract day, month and year from it, for example
select
DAY(activity_date) day,
MONTH((activity_date) month,
YEAR(activity_date) year
from
table_1
will break down the date field in columns you can use...
now, back to your question, it states daily active user, but all you have is sessions, a user could have multiple sessions, so I have no idea, from the context you have shared, how you go about that, and make the avg for each session, makes no sense as data to retrieve, I'll just assume, and serves this answer just to get you started, that you want the avg per day only
knowing how to get the average, let's create a query that has it all together:
select
DAY(activity_date) day,
MONTH((activity_date) month,
YEAR(activity_date) year,
AVG(session_length_seconds) avg
from
table_1
group by
DAY(activity_date),
MONTH((activity_date),
YEAR(activity_date)
will output the average of session_length_seconds per day/month/year
the group by part, you need to have as many fields you have in the select but that do not do any calculation, like sum, count, etc... in our case avg does calculation, so we don't want to group by that value, but we do want to group by the other 3 values, so we have a 3 columns with day, month and year. You can also use concat to join day, month and year into just one string if you prefer...
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 table that has a column that is called scores and another one that is called date_time
I am trying to find out for each 5 minute time increment how many I have that are above a certain score. I want to ignore the date portion completely and just base this off of time.
This is kind of like in a stats program where they display your peak hours with the only difference that I want to go is detailed as 5 minute time segments.
I am still fairly new at MySQL and Google seems to be my best companion.
What I have found so far is:
SELECT id, score, date_time, COUNT(id)
FROM data
WHERE score >= 500
GROUP BY TIME(date_time) DIV 300;
Would this work or is there a better way to do this.
I don't think your query would work. You need to do a bit more work to get the time rounded to 5 minute intervals. Something like:
SELECT SEC_TO_TIME(FLOOR(TIME_TO_SEC(time(date_time))/300)*300) as time5, COUNT(id)
FROM data
WHERE score >= 500
GROUP BY SEC_TO_TIME(FLOOR(TIME_TO_SEC(time(date_time))/300)*300)
ORDER BY time5;
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
;