MySQL search between date, but also between time ranges - mysql

I have a table with datetime like this:
2018-08-08 00:00:00
2018-08-08 03:00:00
2018-08-08 06:00:00
2018-08-08 09:00:00
2018-08-08 12:00:00
2018-08-08 15:00:00
2018-08-08 18:00:00
2018-08-08 21:00:00
2018-08-09 00:00:00
2018-08-09 03:00:00
2018-08-09 06:00:00
2018-08-09 09:00:00
2018-08-09 12:00:00
I search for rows between CURDATE() + INTERVAL 1 DAY and CURDATE() + INTERVAL 4 DAY, but also only at 06:00:00 and 12:00:00.
How can I select this hours?
Regards
Wishy

You can use time() to extract the time.
...
WHERE ...
AND time(nmuloc) BETWEEN time '6:00:00'
AND time '12:00:00'
...

Related

Fetch first weekday and last weekday from table in MySQL

I have a schedule table in mysql. I want to fetch first day and last day of the table as per country code.
Table
COUNTRY_CODE
IS_WORKING
FROM_TIME
END_TIME
DAY
IN
no
NULL
NULL
Sunday
IN
yes
09:00:00
18:00:00
Monday
IN
yes
09:00:00
18:00:00
Tuesday
IN
yes
09:00:00
18:00:00
Wednesday
IN
yes
09:00:00
18:00:00
Thursday
IN
yes
09:00:00
18:00:00
Friday
IN
no
NULL
NULL
Saturday
UAE
yes
10:00:00
19:00:00
Sunday
UAE
yes
10:00:00
19:00:00
Monday
UAE
yes
10:00:00
19:00:00
Tuesday
UAE
yes
10:00:00
19:00:00
Wednesday
UAE
yes
10:00:00
19:00:00
Thursday
UAE
no
NULL
NULL
Friday
UAE
no
NULL
NULL
Saturday
UK
yes
09:00:00
18:00:00
Sunday
UK
yes
09:00:00
18:00:00
Monday
UK
yes
09:00:00
18:00:00
Tuesday
UK
yes
09:00:00
18:00:00
Wednesday
UK
yes
09:00:00
18:00:00
Thursday
UK
yes
09:00:00
18:00:00
Friday
UK
no
NULL
NULL
Saturday
I want result as per below :
COUNTRY_CODE
START_WORKING_DAY
END_WORKING_DAY
IN
Monday
Friday
UAE
Sunday
Thursday
UK
Sunday
Friday
I think this is simply:
select COUNTRY_CODE,
dayname(min(str_to_date(concat(197001,DAY),"%X%V%W"))) start_working_day,
dayname(max(str_to_date(concat(197001,DAY),"%X%V%W"))) end_working_day
from test
where IS_WORKING="yes"
group by 1

Getting Contiguous Shifts

I have a table with the following fields:
-id (int)
-team_id(int)
-user_id (int)
-date (date)
-start_time(time)
-end_time(time)
Running this query:
SELECT * FROM `shifts` WHERE user_id = 1 order by team_id, date, start_time;
Gives me the following result:
id team_id user_id date start_time end_time
1 3 1 2017-11-29 10:00:00 11:00:00
2 3 1 2017-11-29 11:00:00 12:00:00
5 3 1 2017-11-29 12:00:00 13:00:00
6 3 1 2017-11-29 16:00:00 17:00:00
7 3 1 2017-11-29 17:00:00 18:00:00
10 4 1 2017-11-30 17:00:00 18:00:00
8 4 1 2017-11-30 19:00:00 20:00:00
51 5 1 2017-11-30 18:00:00 19:00:00
11 5 1 2017-11-30 19:00:00 20:00:00
I am looking for a query that gives me the following result with contiguous start_time - end_time:
driver_id team_id date contiguous_start_time contiguous_end_time hours
1 3 2017-11-29 10:00:00 13:00:00 3
1 3 2017-11-29 16:00:00 18:00:00 2
1 4 2017-11-30 17:00:00 18:00:00 1
1 4 2017-11-30 19:00:00 20:00:00 1
1 5 2017-11-30 18:00:00 20:00:00 2
Any and all help is appreciated.

How to identify intervel of time stamps in a column

I have table with 2 columns,we can see that Rowid(1-5) its 15 min interval & Rowid(6-10) its 1 hour interval , how to add a new column whichn shows the time intervals.
Rowid Date Value
1 01-Nov-16 00:00:00 716
2 01-Nov-16 00:15:00 716
3 01-Nov-16 00:15:00 716
4 01-Nov-16 00:45:00 717
5 01-Nov-16 01:00:00 716
6 01-Nov-16 01:00:00 34
7 01-Nov-16 02:00:00 66
8 01-Nov-16 03:00:00 717
9 01-Nov-16 04:00:00 717
10 01-Nov-16 05:00:00 717
O/P :
Rowid Date Value Interval_gap
1 01-Nov-16 00:00:00 716 15 mins
2 01-Nov-16 00:15:00 716 15 mins
3 01-Nov-16 00:15:00 716 15 mins
4 01-Nov-16 00:45:00 717 15 mins
5 01-Nov-16 01:00:00 716 15 mins
6 01-Nov-16 01:00:00 34 1 hour
7 01-Nov-16 02:00:00 66 1 hour
8 01-Nov-16 03:00:00 717 1 hour
9 01-Nov-16 04:00:00 717 1 hour
10 01-Nov-16 05:00:00 717 1 hour
Thanks in advance
This isn't an "answer" but I need formatting to make a point:
Please check your expected result. I assume "value" means the rows are related, and that for any sequence the first row cannot produce a result
e.g.
Rowid Date Value Interval_gap
1 01-Nov-16 00:00:00 716 NULL << nothing prior to this row, so no calculation
2 01-Nov-16 00:15:00 716 15 mins
3 01-Nov-16 00:15:00 716 0 mins
5 01-Nov-16 01:00:00 716 45 mins
6 01-Nov-16 01:00:00 34 NULL << nothing prior to this row, so no calculation
7 01-Nov-16 02:00:00 66 NULL << nothing prior to this row, so no calculation
4 01-Nov-16 00:45:00 717 NULL << nothing prior to this row, so no calculation
8 01-Nov-16 03:00:00 717 2 Hours 45 mins
9 01-Nov-16 04:00:00 717 1 hour
10 01-Nov-16 05:00:00 717 1 hour
Round 2
Rowid Date Value Interval_gap Named
1 01-Nov-16 00:00:00 716 NULL ABC
2 01-Nov-16 00:15:00 716 15 mins ABC
3 01-Nov-16 00:15:00 716 15 mins ABC
4 01-Nov-16 00:45:00 717 15 mins ABC
5 01-Nov-16 01:00:00 716 15 mins ABC
6 01-Nov-16 01:00:00 34 NULL XYZ
7 01-Nov-16 02:00:00 66 1 hour XYZ
8 01-Nov-16 03:00:00 717 1 hour XYZ
9 01-Nov-16 04:00:00 717 1 hour XYZ
10 01-Nov-16 05:00:00 717 1 hour XYZ

MySQL: Get start & end timestamp for each day

Similar to the question at MYSQL: Find Start and End Timestamp of a consecutive count, I have a table with similar data:
StatusTime | Reading
2014-01-01 00:00 | 255
2014-01-01 01:00 | 255
2014-01-01 02:00 | 255
2014-01-01 03:00 | 255
2014-01-01 04:00 | 255
2014-01-01 05:00 | 241
2014-01-01 06:00 | 189
2014-01-01 07:00 | 100
2014-01-01 08:00 | 20
2014-01-01 09:00 | 0
2014-01-01 10:00 | 1
2014-01-01 11:00 | 1
2014-01-01 12:00 | 0
2014-01-01 13:00 | 21
2014-01-01 14:00 | 1
2014-01-01 15:00 | 0
2014-01-01 16:00 | 12
2014-01-01 17:00 | 63
2014-01-01 18:00 | 102
2014-01-01 19:00 | 198
2014-01-01 20:00 | 255
2014-01-01 21:00 | 255
2014-01-01 22:00 | 255
2014-01-01 23:00 | 255
2014-01-02 00:00 | 255
2014-01-02 01:00 | 255
2014-01-02 02:00 | 255
2014-01-02 03:00 | 255
2014-01-02 04:00 | 255
2014-01-02 05:00 | 208
2014-01-02 06:00 | 100
2014-01-02 07:00 | 48
2014-01-02 08:00 | 0
2014-01-02 09:00 | 0
I'd like to extract the start and end timestamps where readings go above 50 and below 50, respectively. I just can't get my head around the SQL presented in the other answer!
Thanks in advance for your help.
If it only goes below/above again once per day, you can make the query quite simple; just find the min and max time where it's below, grouping by date.
SELECT
DATE(statustime) statusdate,
MIN(CASE WHEN reading<50 THEN statustime ELSE NULL END) start_time,
MAX(CASE WHEN reading<50 THEN statustime ELSE NULL END) end_time
FROM myTable
GROUP BY statusdate
An SQLfiddle to test with.
Try this code:
select *
(select max(StatusTime) from tbl where Reading > 50) above_50,
(select min(StatusTime) from tbl where Reading < 50) bellow_50;
Something like this?
SELECT DISTINCT MAX(StatusTime), MIN(StatusTime)
FROM table_name
WHERE Reading < 50 OR Reading > 50;

mysql adding a new column to label the time-series data points for each id

I got time-series data storing in a table as follows.
id, timestamp, value
d1 2000-01-01 00:00:00 1
d1 2000-01-01 01:00:00 2
d1 2000-01-01 02:00:00 3
d1 2000-01-01 03:00:00 4
d1 2000-01-01 04:00:00 5
...
d2 2000-01-01 00:00:00 25
d2 2000-01-01 01:00:00 26
d2 2000-01-01 02:00:00 27
d2 2000-01-01 03:00:00 28
d2 2000-01-01 04:00:00 29
d2 2000-01-01 05:00:00 30
...
d3 2000-01-01 00:00:00 49
d3 2000-01-01 01:00:00 50
d3 2000-01-01 02:00:00 51
d3 2000-01-01 03:00:00 52
d3 2000-01-01 04:00:00 53
What I need to do is to add another column to the table as follows.
The purpose is to label the number of data points for each id for further process.
id, timestamp, value, counter
d1 2000-01-01 00:00:00 1 1
d1 2000-01-01 01:00:00 2 2
d1 2000-01-01 02:00:00 3 3
d1 2000-01-01 03:00:00 4 4
d1 2000-01-01 04:00:00 5 5
...
d2 2000-01-01 00:00:00 25 1
d2 2000-01-01 00:00:00 26 2
d2 2000-01-01 01:00:00 27 3
d2 2000-01-01 02:00:00 28 4
d2 2000-01-01 03:00:00 29 5
d2 2000-01-01 04:00:00 30 6
...
d3 2000-01-01 00:00:00 49 1
d3 2000-01-01 01:00:00 50 2
d3 2000-01-01 02:00:00 51 3
d3 2000-01-01 03:00:00 52 4
d3 2000-01-01 04:00:00 53 5
How to realise it using mysql command?
Thanks.
SELECT
yt.*
, IF(#prev=id, #counter:=#counter + 1, #counter:=1) as counter
, #prev:=id
FROM
yourTable yt
, (SELECT #counter:=0, #prev:=NULL) vars
order by id, `timestamp`
See it live here.