I have table where wind speed is inserted every couple of minutes. Lets say i would then like to calculate average wind speed for certain time window for each day.
I could use SQL command like
select ROUND(avg(data.wind),1) wind
FROM data
WHERE station in(109)
&& hour(data.datum)>=7
&& hour(data.datum)<= 8
group by month(data.datum), day(data.datum)
This works fine but the problem is i would like to use dynamic time slot based on sunrise time. I found SQL function to calculate sunrise time for a specific date but i dont know how to use dynamic time window for each day in group by in my sample.
For sunrise i would use https://github.com/curzon01/mysql_SunRiseSet
which uses command like
select SunRiseSet(yyyy-mm-dd, 45.299, 13.571, 'nautical', 'rise');
For example i would like range from sunrise - 1 hour upto sunrise + 1 hour. This would mean
day1 7:20-8:20
day2 7:21-8:21
day3 7:23-8:23
etc.
Is this possible in one command?
SELECT ROUND(AVG(data.wind), 1) AS wind
FROM data
WHERE station IN (109)
AND TIME_TO_SEC(TIMEDIFF(TIME(data.datum), SunRiseSet(data.datum, ...)))
BETWEEN -3600 AND 3600
GROUP BY MONTH(data.datum), DAY(data.datum);
I assume you can get the coordinates of the respective station to put in where I have left .... Maybe join to another table where you store the station details like location?
Related
Show, along with the title of the film and the year, what the running times of science fiction films would be like, adding 20 minutes to the runtime.
I need to make a query that returns this, and I didn't find anywhere how to add these 20 minutes, in the duration I put TIME as the data type, and when I put the data I put it like this 00:00:00. Now I don't know how to do this sum.
SELECT title, year, duration
FROM film
WHERE genero = "fiction"
GROUP BY duration;
I know until there, the problem is the sum. Does anyone know how to do it??
Use the DATE_ADD() function to add an interval to a date or time.
SELECT title, year, TIME(DATE_ADD(duration, INTERVAL 20 MINUTE)) AS running_time
FROM film
WHERE genero = 'fiction'
There's no need for GROUP BY since you want to show all the films. Did you mean ORDER BY duration?
I have data per day in my database mysql. What I want to do is to select the km field in the current time in the day minus the km field in start of the day (current day time :00:00 am).
I tried this but it didnt work:
SELECT km from position where serverTime=startOfTheDay
SELECT km from position where serverTime=currentTime()
These two requests don't work. I just need an idea of what I need to do.
Also, I need to minus the last km from the first one, help please??
You can try this approach - it finds the difference between the smallest and largest km for each day.
select min(serverTime) as start,
max(serverTime) as stop,
max(km) - min(km) as km_difference
from position
group by date(serverTime);
First one (and only works if you have properly defined the col serverTime): SELECT... curdate()
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`;
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;
I'm reasonably new to Access and having trouble solving what should be (I hope) a simple problem - think I may be looking at it through Excel goggles.
I have a table named importedData into which I (not so surprisingly) import a log file each day. This log file is from a simple data-logging application on some mining equipment, and essentially it saves a timestamp and status for the point at which the current activity changes to a new activity.
A sample of the data looks like this:
This information is then filtered using a query to define the range I want to see information for, say from 29/11/2013 06:00:00 AM until 29/11/2013 06:00:00 PM
Now the object of this is to take a status entry's timestamp and get the time difference between it and the record on the subsequent row of the query results. As the equipment works for a 12hr shift, I should then be able to build a picture of how much time the equipment spent doing each activity during that shift.
In the above example, the equipment was in status "START_SHIFT" for 00:01:00, in status "DELAY_WAIT_PIT" for 06:08:26 and so-on. I would then build a unique list of the status entries for the period selected, and sum the total time for each status to get my shift summary.
You can use a correlated subquery to fetch the next timestamp for each row.
SELECT
i.status,
i.timestamp,
(
SELECT Min([timestamp])
FROM importedData
WHERE [timestamp] > i.timestamp
) AS next_timestamp
FROM importedData AS i
WHERE i.timestamp BETWEEN #2013-11-29 06:00:00#
AND #2013-11-29 18:00:00#;
Then you can use that query as a subquery in another query where you compute the duration between timestamp and next_timestamp. And then use that entire new query as a subquery in a third where you GROUP BY status and compute the total duration for each status.
Here's my version which I tested in Access 2007 ...
SELECT
sub2.status,
Format(Sum(Nz(sub2.duration,0)), 'hh:nn:ss') AS SumOfduration
FROM
(
SELECT
sub1.status,
(sub1.next_timestamp - sub1.timestamp) AS duration
FROM
(
SELECT
i.status,
i.timestamp,
(
SELECT Min([timestamp])
FROM importedData
WHERE [timestamp] > i.timestamp
) AS next_timestamp
FROM importedData AS i
WHERE i.timestamp BETWEEN #2013-11-29 06:00:00#
AND #2013-11-29 18:00:00#
) AS sub1
) AS sub2
GROUP BY sub2.status;
If you run into trouble or need to modify it, break out the innermost subquery, sub1, and test that by itself. Then do the same for sub2. I suspect you will want to change the WHERE clause to use parameters instead of hard-coded times.
Note the query Format expression would not be appropriate if your durations exceed 24 hours. Here is an Immediate window session which illustrates the problem ...
' duration greater than one day:
? #2013-11-30 02:00# - #2013-11-29 01:00#
1.04166666667152
' this Format() makes the 25 hr. duration appear as 1 hr.:
? Format(#2013-11-30 02:00# - #2013-11-29 01:00#, "hh:nn:ss")
01:00:00
However, if you're dealing exclusively with data from 12 hr. shifts, this should not be a problem. Keep it in mind in case you ever need to analyze data which spans more than 24 hrs.
If subqueries are unfamiliar, see Allen Browne's page: Subquery basics. He discusses correlated subqueries in the section titled Get the value in another record.