Filter Rows with time difference - mysql

I have a "readings" table with columns
timestamp as DateTime
channel_id as integer
value as Decimal
test_id as integer
Now i want to filter rows by test_id which anybody can do. Then in the result number of rows is too much. Suppose if i use channel number 2,3 and 5 for my test_id 17 and if I log data for every second then there are 10k rows.
When I plot the graph there where will so many data that the graph lines are not visible clearly, so to make them visible I need to filter out some rows.
I need some help in filtering of rows with time difference between the two records should be few seconds lets say 10 seconds. In this the data is not consecutive.
Any help will be greatly appreciated.
The sample data will be as follows:
24-05-2016 08:00:55 am | 2 | 10.23 | 17
24-05-2016 08:00:55 am | 3 | 100.23 | 17
24-05-2016 08:00:55 am | 5 | 12.23 | 17
24-05-2016 08:00:56 am | 2 | 09.23 | 17
24-05-2016 08:00:56 am | 3 | 12.23 | 17
24-05-2016 08:00:56 am | 5 | 11.23 | 17
24-05-2016 08:00:57 am | 2 | 09.23 | 17
24-05-2016 08:00:57 am | 3 | 01.23 | 17
24-05-2016 08:00:57 am | 5 | 11.23 | 17
24-05-2016 08:00:58 am | 2 | 09.23 | 17
24-05-2016 08:00:58 am | 3 | 01.23 | 17
24-05-2016 08:00:58 am | 5 | 11.23 | 17

Instead of filtering you can aggregate data for the period using GROUP BY functionality. For example, 10-second periods can be calculated from the timestamps using this formula:
ROUND(UNIX_TIMESTAMP(timestamp)/10)
So, this formula can be added to GROUP BY query, so query can aggregate data on that period:
SELECT test_id,channel_id,ROUND(UNIX_TIMESTAMP(timestamp)/10),
min(value), max(value), avg(value),count(*)
FROM your_table
WHERE some_conditions
GROPU BY test_id,channel_id,ROUND(UNIX_TIMESTAMP(timestamp)/10)

Related

Multiple DateTime range MySql

I have a table
---------+-----------+------------------+--------------------+
| id | user_id | start_date_time | end_date_time |
+---------+---------+-------------------+--------------------+
| 1 | 11 |2019-11-17 20:10:00|2019-11-17 21:05:00 |
| 2 | 11 |2019-11-17 20:18:00|2019-11-17 20:35:00 |
| 3 | 11 |2019-11-17 20:32:00|2019-11-17 21:18:00 |
| 4 | 11 |2019-11-17 20:40:00|2019-11-17 20:50:00 |
| 5 | 11 |2019-11-17 20:45:00|2019-11-17 21:20:00 |
| | | | |
+---------+---------+-------------------+--------------------+
Scenario 1 - If i query for all greater than '2019-11-17 20:18:00' I need to get all records.
Scenario 2 - If i query for all possible dates greater or equals '2019-11-17 21:18:00' It should return record 3 and 5.
For any given time it should look for Start_date_time and End_date_time where given time should be considered as start time and it should look for appropriate end_date_time and output the result.
In a nut shell input time should be taken as starting range and it should look for End_date_time and give me all values between.
How can i accomplish this?
i tried the following ways on db-fiddle https://www.db-fiddle.com/f/bPk1CYioL6cVasStZKzQ4j/7
if i query all records from a given time eg(2019-11-17 20:18:00) the input should be taken as a start datetime of range and look for the most greatest end_date_time and give me the records between them. Example 2019-11-17 20:18:00 this input takes range between input as val1 of range 2019-11-17 20:18:00 to 2019-11-17 21:20:00 the highest end date and give me all records between. And if i query with input 2019-11-17 21:05:00 this should take start range val1 as 2019-11-17 21:05:00 and 2019-11-17 21:20:00 output 1,3,5 records.
The requirement (with the results that you expect) is as simple as that:
select *
from times
where ? <= end_date_time
Replace ? with the datetime that you want to query.
See the demo.

Properly SQL query

I need to skip results with high price per day. I've got a table like this:
+------+-------------+-------+
| days | return_date | value |
+------+-------------+-------+
| 2 | 2017-12-27 | 15180 |
| 3 | 2017-12-28 | 14449 |
| 4 | 2017-12-29 | 13081 |
| 5 | 2017-12-30 | 11203 |
| 6 | 2017-12-31 | 9497 |
| 6 | 2017-12-31 | 9442 |
+------+-------------+-------+
How can I print only the lowest price for 6 days (9442 in this example).
We can use a GROUP BY clause and an aggregate function. For example:
SELECT t.days
, t.return_date
, MIN(t.value) AS min_value
FROM mytable t
GROUP
BY t.days
, t.return_date
This doesn't really "skip" rows. It accesses all the rows that satisfy the conditions in the WHERE clause (in this example, every row in the table). Then MySQL collapses rows into groups (in this example, rows with identical values of days and return_date get put into a group. The MIN(t.value) aggregate function selects out the minimum (lowest) value out of the group.
The query above is just an example of one approach of satisfying a particular specification.

SQL - select x entries within a timespan

I'm creating a database (in MySQL) with a table of measurements. For each measurement I want to store the DateTime it came in. For showing plots within an app for different intervals (measurements of the day/week/month/year) I want sample the data points I have, so I can return e. g. 30 data points for the whole year as well as for the day/hour. This is the same as done with stock price graphs:
stock price plot for 1 day
vs
stock price plot for 1 month
As you can see, the amount of data points is the same in both pictures.
So how can I select x entries within a timespan in MySQL via SQL?
My data looks like this:
+====+====================+=============+==========+
| id | datetime | temperature | humidity |
+====+====================+=============+==========+
| 1 | 1-15-2016 00:30:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 2 | 1-15-2016 00:35:00 | 19 | 41 |
+----+--------------------+-------------+----------+
| 3 | 1-15-2016 00:40:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 4 | 1-15-2016 00:45:00 | 20 | 42 |
+----+--------------------+-------------+----------+
| 5 | 1-15-2016 00:50:00 | 21 | 42 |
+----+--------------------+-------------+----------+
| 6 | 1-15-2016 00:55:00 | 20 | 43 |
+----+--------------------+-------------+----------+
| 7 | 1-15-2016 01:00:00 | 21 | 43 |
+====+====================+=============+==========+
Let's say, I always want two data points (in reality a lot more). So for the last half hour I want the database to return data point 1 and 4, for the last ten minutes I want it to return 6 and 7.
Thanks for helping!
PS: I'm sorry for any errors in my English
OK, assuming a very simple systematic approach, you can get the first and last entry for any defined period:
select *
from table
where mydatetime =
(select
max(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
OR mydatetime =
(select
min(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
I believe your answer can be found at the following location:
https://stackoverflow.com/a/1891796/7176046
If you are looking to filter out any items not within your date/time your query would use:
Select * from table where Date/Time is (What you want to sort by)

MySQL - How to subtract two datetime from the same table

I need help on a small problem with a subtraction in the same table and column
Well, iam creating a view, but the aplication generated the results of used time in tha same table and column.
My table have the following columns: id,field_id,object_id and value_date.
| ID | FIELD_ID | OBJECT_ID | VALUE_DATE |
| 55 | 4 | 33 | 2016-12-18 19:02:00 |
| 56 | 5 | 33 | 2016-12-18 19:12:00 |
| 57 | 4 | 35 | 2016-12-18 19:30:00 |
| 58 | 5 | 35 | 2016-12-18 20:00:00 |
I do not have much knowledge in sql, but i have tried some functions like timestampdiff, period_siff and others examples in stackoverflow.com.
Someone help me to subtract ID 56 with field_id 5 by line with ID 55 and field_id 4 in object_id 33 in SQL to bring the result in minutes. Ex: 10 or 00:10:00
An article about this problem would already help me. Thank you very much!
Lets assume that you want result to be in day format then query will be :
SELECT DATEDIFF(day,startDate,endDate) AS 'Day'
FROM table1;
Find complete example here
The soluction is below:
select TIMESTAMPDIFF(MINUTE,F1.value_date,F2.value_date) as minutes, F1.value_date,F2.value_date,F1.object_id,F2.object_id,F1.field_id,F2.field_id
from otrs_tst.dynamic_field_value F1
join otrs_tst.dynamic_field_value F2 on F1.object_id = F2.object_id
where F1.field_id in ('4','5')
and F2.field_id in ('4','5')
and F2.field_id <> F1.field_id
and F1.field_id < F2.field_id
group by F1.object_id,F2.field_id

Duplication/Updation of data in mysql

I want to get the sum of the data for every 5 minutes.
I have 15 motes.
for ,suppose in the first 5 minutes only some motes are queried and in the next 5 minutes other some motes are queried.
Now,In the second 5 minutes I need the data of the motes which are not queried in that 5minutes also
ie.,in the first 5minutes moteid's 1,2,3,4,9,12,14 are queried and in the second minutes moteid's 1,5,6,7,9,13,14 are queried.
In the second 5 minutes,I need the data to be updated for the one's which are not queried also.Is it possible to get the data from the previous 5 minutes
moteid2 | 28 | 2012-09-25 17:45:43 | |
moteid4 | 65 | 2012-09-25 17:45:49 | |
moteid3 | 66 | 2012-09-25 17:45:51 | |
moteid6 | 25 | 2012-09-25 17:45:56 | |
moteid5 | 29 | 2012-09-25 17:45:58 | |
moteid7 | 30 | 2012-09-25 17:46:05 | |
moteid4 | 95 | 2012-09-25 17:50:29 | |
moteid6 | 56 | 2012-09-25 17:50:35 | |
moteid5 | 58 | 2012-09-25 17:50:36 | |
moteid4 | 126 | 2012-09-25 17:55:08 |
In the first 5 minutes moteid2, moteid3 are queried, but after that in the next 5minutes they are not queried. Even If they are not being queried i want the same previous queried value to be kept now.
I'm assuming the table name is motes. In this case the following query displays all unique motesid for the records which present in the whole table but were not queried in last 5 minutes:
select distinct m.motesid
from motes m
where not exists (
select *
from motes m1
where
m1.moteid = m.motesid and
m1.date > SUBTIME(CURTIME(), '0:05:00')
)