How to get data of the current time from mysql database - mysql

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()

Related

MYSQL group by with dynamic where

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?

get average hours in php language

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;

mysql timediff no proper output when endtime is 24hrs

I'm using datatype time to calculate the class taken timings. For calculation I use TIMEDIFF(endtime,starttime).
Query
SELECT TIMEDIFF('00:26:08','21:58:18') FROM students_session WHERE id='#'
I'm not getting the proper o/p which is 02:27:50. Instead I get -21:32:10, which is wrong.
How to rectify this?
The issue is that you know that '00:26:08' is after '21:58:18' (following morning), but MySQL is not aware, thus the result is correct from MySQL point of view.
You either need to provide a date part, where the end_date falls to the next day, or you need to add 24 hours (1 day) to the end_date. These will tell MySQL that the end_date is greater than the start_date and you will get the results you expect.
SELECT TIMEDIFF(timeadd('00:26:08','24:00:00'),'21:58:18') from students_session where id='#'

Calculating the tendency of a value over time

I've got a table with (amongst other values) the temperature for the last 4 hours. When making a graph of it I can see the 'tendency' of the graph on an eye blink:
The thick red line obviously has a negative direction. While the green line has a positive direction.
How can I calculate this 'direction' value of the last 3 hours worth of data. This data can be retreived from the database with the following sql-statement:
SELECT temp FROM weather WHERE time_utc => NOW() - INTERVAL 3 HOUR
Is there a function like AVG() or something to calculate this or am I overthinking this?
What about this :
SELECT HOUR(time_utc) as hour_group, AVG(temp)
FROM weather
WHERE time_utc => NOW() - INTERVAL 3 HOUR
GROUP BY hour_group
This way you divide your measures in hour block and can compare the first with the last?

SQL: time_diff returns wrong value when value is not in the same year

With the following query I want to select all adverts which have exceeded the allowed time of duration (saved as seconds) and are not active anymore:
SELECT *
FROM adverts
WHERE TIME_TO_SEC(timediff(now(),adverts.stamp_created))>adverts.duration
My problem: If the time when the advert was created (= stamp_created, TIMESTAMP) is in this year, this query works correctly and the time_to_sec function gives back the correct time difference in seconds.
However, if the time when the advert was created is in a different year than the current one, for example in 2014, I get a wrong value for the time difference and the query does not work. What could be the reason? How do I need to adjust my query?
How about rephrasing the condition by adding seconds, rather than taking a difference?
SELECT *
FROM adverts a
WHERE DATE_ADD(a.stamp_created, interval a.duration second) > NOW();