How to identify intervel of time stamps in a column - mysql

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

Related

MySQL search between date, but also between time ranges

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'
...

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 join two table with different value with out duplicate using sql server 2014 management

I have two tables:
table_3
SiNo date no ofemplyoe amount contractor
1 2015-03-01 00:00:00 5 500.00 Rgp
2 2015-03-01 00:00:00 5 500.00 Dwl
3 2015-03-02 00:00:00 5 500.00 Rgp
4 2015-03-01 00:00:00 5 500.00 Dwl
table_2
SiNo contractor amountpaid
1 RGP 100
2 RGP 200
3 Dwl 100
4 Dwl 200
and I want to join & to be displayed like this type:
SiNo date no ofemplyoe amount contractor amountpaid**
1 2015-03-01 00:00:00 5 500.00 Rgp
2 2015-03-01 00:00:00 5 500.00 Dwl
3 2015-03-02 00:00:00 5 500.00 Rgp
4 2015-03-01 00:00:00 5 500.00 Dwl
5 2015-03-03 00:00:00 Rgp 100
5 2015-03-04 00:00:00 Dwl 100
7 2015-03-03 00:00:00 Dwl 200
8 2015-03-04 00:00:00 Rgp 200
can any one help me to achieve this?

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.