Getting Contiguous Shifts - mysql

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.

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

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 average of datetime

I have a table having fields id, cust_id, ord_id, ord_date(datetime datatype). In table, I have data like this..
id cust_id ord_id ord_date prod_id
1 1 1 2015-12-01 00:00:00 1
2 1 1 2015-12-01 00:00:00 2
3 1 1 2015-12-01 00:00:00 3
4 2 1 2015-12-01 00:00:00 1
5 1 3 2015-12-03 00:00:00 1
6 1 3 2015-12-03 00:00:00 2
7 1 2 2015-12-02 00:00:00 1
8 3 1 2015-12-03 00:00:00 1
9 3 1 2015-12-03 00:00:00 2
10 2 2 2015-12-07 00:00:00 1
12 2 2 2015-12-07 00:00:00 2
13 3 2 2015-12-10 00:00:00 1
14 1 4 2015-12-12 00:00:00 1
15 3 3 2015-12-15 00:00:00 1
I have to get data of average order time of each customers with last ord_id (max ord_id) and should be order by cust_id ASC, ord_id DESC.
I want output like this using MYSQL query.
cust_id ord_id ord_date ord_avg_day
1 4 2015-12-12 00:00:00 3
2 2 2015-12-07 00:00:00 3
3 3 2015-12-15 00:00:00 4
I have tried this, but failed as it shows average time 0.
SELECT cust_id, ord_id, ord_date, AVG(TIME_TO_SEC(ord_date)) AS ord_avg_day FROM tableName GROUP BY cust_id, ord_id ORDER BY cust_id, ord_id DESC
I know that it will be easy using normalization. But I have no option for it. I have to work with only this table.
If anyone knows the solution, then answer will be appreciate.
here a simple way to do it. There are some output that you not using. You can delete it. Its only to test.
SELECT
cust_id
, count(*) AS ord_id
, min(ord_date) AS first_order
, max(ord_date) AS last_order
, (DATEDIFF(max(ord_date) , min(ord_date)) ) / (count(*)-1) AS ord_avg_day
FROM (
SELECT *
FROM myorder
GROUP BY ord_date,cust_id
) AS tmp
GROUP BY cust_id;
Result
+---------+--------+---------------------+---------------------+-------------+
| cust_id | ord_id | first_order | last_order | ord_avg_day |
+---------+--------+---------------------+---------------------+-------------+
| 1 | 4 | 2015-12-01 00:00:00 | 2015-12-12 00:00:00 | 3.6667 |
| 2 | 2 | 2015-12-01 00:00:00 | 2015-12-07 00:00:00 | 6.0000 |
| 3 | 3 | 2015-12-03 00:00:00 | 2015-12-15 00:00:00 | 6.0000 |
+---------+--------+---------------------+---------------------+-------------+
3 rows in set (0.00 sec)
I have correct a error.
Please let me know if it works for you

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