MySQL group rows by progressive date in consecutive rows
up vote
2
down vote
favorite
This is my table
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 18/01/2013 16:26:47 |
| 1 | A | I | 18/01/2013 16:26:47 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 21/06/2013 16:45:37 |
| 1 | B | II | 21/06/2013 16:45:37 | 23/06/2013 09:45:00 |
| 1 | B | II | 23/06/2013 09:45:00 | 23/06/2013 11:57:18 |
| 1 | B | II | 23/06/2013 11:57:18 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 25/06/2013 17:57:03 |
| 1 | B | II | 25/06/2013 17:57:03 | 28/06/2013 09:45:00 |
| 1 | B | II | 28/06/2013 09:45:00 | 28/06/2013 12:41:28 |
| 1 | B | II | 28/06/2013 12:41:28 | 03/07/2013 20:19:29 |
| 1 | B | II | 03/07/2013 20:19:29 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 10/07/2013 10:55:38 |
| 1 | B | II | 10/07/2013 13:29:58 | 09/08/2013 17:16:13 |
| 1 | B | II | 09/08/2013 17:16:13 | 27/09/2013 14:45:31 |
| 1 | B | II | 27/09/2013 14:45:31 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
My aim is to group rows by ID_1, ID_2, ID_3 when exit
of previous row is equal to enter
of next row, and I also want to change exit
with the the last consecutive exit
The resul should be
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
Is it possible to have it in MySQL?
mysql
|
show 3 more comments
up vote
2
down vote
favorite
This is my table
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 18/01/2013 16:26:47 |
| 1 | A | I | 18/01/2013 16:26:47 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 21/06/2013 16:45:37 |
| 1 | B | II | 21/06/2013 16:45:37 | 23/06/2013 09:45:00 |
| 1 | B | II | 23/06/2013 09:45:00 | 23/06/2013 11:57:18 |
| 1 | B | II | 23/06/2013 11:57:18 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 25/06/2013 17:57:03 |
| 1 | B | II | 25/06/2013 17:57:03 | 28/06/2013 09:45:00 |
| 1 | B | II | 28/06/2013 09:45:00 | 28/06/2013 12:41:28 |
| 1 | B | II | 28/06/2013 12:41:28 | 03/07/2013 20:19:29 |
| 1 | B | II | 03/07/2013 20:19:29 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 10/07/2013 10:55:38 |
| 1 | B | II | 10/07/2013 13:29:58 | 09/08/2013 17:16:13 |
| 1 | B | II | 09/08/2013 17:16:13 | 27/09/2013 14:45:31 |
| 1 | B | II | 27/09/2013 14:45:31 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
My aim is to group rows by ID_1, ID_2, ID_3 when exit
of previous row is equal to enter
of next row, and I also want to change exit
with the the last consecutive exit
The resul should be
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
Is it possible to have it in MySQL?
mysql
1
What is your MySQL server version ? Is there any Primary key in your table ? Without that we really cant define "order", as data is unordered
– Madhur Bhaiya
Nov 22 at 14:01
This looks like a gaps and islands problem to me, or at least a variant of it. If you are using MySQL 8+ or later, then you may take advantage of analytic functions, which would really help here.
– Tim Biegeleisen
Nov 22 at 14:05
MySQL 5.7. There is no pk in my table
– Gongo82
Nov 22 at 14:06
2
If you have no PK, then you have no table.
– Strawberry
Nov 22 at 14:09
1
@Arth It depends on the engine (I think)
– Strawberry
Nov 22 at 18:21
|
show 3 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This is my table
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 18/01/2013 16:26:47 |
| 1 | A | I | 18/01/2013 16:26:47 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 21/06/2013 16:45:37 |
| 1 | B | II | 21/06/2013 16:45:37 | 23/06/2013 09:45:00 |
| 1 | B | II | 23/06/2013 09:45:00 | 23/06/2013 11:57:18 |
| 1 | B | II | 23/06/2013 11:57:18 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 25/06/2013 17:57:03 |
| 1 | B | II | 25/06/2013 17:57:03 | 28/06/2013 09:45:00 |
| 1 | B | II | 28/06/2013 09:45:00 | 28/06/2013 12:41:28 |
| 1 | B | II | 28/06/2013 12:41:28 | 03/07/2013 20:19:29 |
| 1 | B | II | 03/07/2013 20:19:29 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 10/07/2013 10:55:38 |
| 1 | B | II | 10/07/2013 13:29:58 | 09/08/2013 17:16:13 |
| 1 | B | II | 09/08/2013 17:16:13 | 27/09/2013 14:45:31 |
| 1 | B | II | 27/09/2013 14:45:31 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
My aim is to group rows by ID_1, ID_2, ID_3 when exit
of previous row is equal to enter
of next row, and I also want to change exit
with the the last consecutive exit
The resul should be
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
Is it possible to have it in MySQL?
mysql
This is my table
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 18/01/2013 16:26:47 |
| 1 | A | I | 18/01/2013 16:26:47 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 21/06/2013 16:45:37 |
| 1 | B | II | 21/06/2013 16:45:37 | 23/06/2013 09:45:00 |
| 1 | B | II | 23/06/2013 09:45:00 | 23/06/2013 11:57:18 |
| 1 | B | II | 23/06/2013 11:57:18 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 25/06/2013 17:57:03 |
| 1 | B | II | 25/06/2013 17:57:03 | 28/06/2013 09:45:00 |
| 1 | B | II | 28/06/2013 09:45:00 | 28/06/2013 12:41:28 |
| 1 | B | II | 28/06/2013 12:41:28 | 03/07/2013 20:19:29 |
| 1 | B | II | 03/07/2013 20:19:29 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 10/07/2013 10:55:38 |
| 1 | B | II | 10/07/2013 13:29:58 | 09/08/2013 17:16:13 |
| 1 | B | II | 09/08/2013 17:16:13 | 27/09/2013 14:45:31 |
| 1 | B | II | 27/09/2013 14:45:31 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
My aim is to group rows by ID_1, ID_2, ID_3 when exit
of previous row is equal to enter
of next row, and I also want to change exit
with the the last consecutive exit
The resul should be
+------+------+------+---------------------+---------------------+
| ID_1 | ID_2 | ID_3 | enter | exit |
+------+------+------+---------------------+---------------------+
| 1 | A | I | 17/01/2013 15:37:25 | 28/01/2013 11:22:59 |
| 1 | B | II | 13/06/2013 22:23:54 | 25/06/2013 13:35:51 |
| 1 | B | II | 25/06/2013 14:45:21 | 05/07/2013 17:11:30 |
| 1 | B | II | 05/07/2013 21:33:30 | 11/11/2013 12:00:00 |
| 1 | C | III | 09/03/2014 00:14:31 | 05/04/2014 12:12:51 |
+------+------+------+---------------------+---------------------+
Is it possible to have it in MySQL?
mysql
mysql
asked Nov 22 at 14:01
Gongo82
112
112
1
What is your MySQL server version ? Is there any Primary key in your table ? Without that we really cant define "order", as data is unordered
– Madhur Bhaiya
Nov 22 at 14:01
This looks like a gaps and islands problem to me, or at least a variant of it. If you are using MySQL 8+ or later, then you may take advantage of analytic functions, which would really help here.
– Tim Biegeleisen
Nov 22 at 14:05
MySQL 5.7. There is no pk in my table
– Gongo82
Nov 22 at 14:06
2
If you have no PK, then you have no table.
– Strawberry
Nov 22 at 14:09
1
@Arth It depends on the engine (I think)
– Strawberry
Nov 22 at 18:21
|
show 3 more comments
1
What is your MySQL server version ? Is there any Primary key in your table ? Without that we really cant define "order", as data is unordered
– Madhur Bhaiya
Nov 22 at 14:01
This looks like a gaps and islands problem to me, or at least a variant of it. If you are using MySQL 8+ or later, then you may take advantage of analytic functions, which would really help here.
– Tim Biegeleisen
Nov 22 at 14:05
MySQL 5.7. There is no pk in my table
– Gongo82
Nov 22 at 14:06
2
If you have no PK, then you have no table.
– Strawberry
Nov 22 at 14:09
1
@Arth It depends on the engine (I think)
– Strawberry
Nov 22 at 18:21
1
1
What is your MySQL server version ? Is there any Primary key in your table ? Without that we really cant define "order", as data is unordered
– Madhur Bhaiya
Nov 22 at 14:01
What is your MySQL server version ? Is there any Primary key in your table ? Without that we really cant define "order", as data is unordered
– Madhur Bhaiya
Nov 22 at 14:01
This looks like a gaps and islands problem to me, or at least a variant of it. If you are using MySQL 8+ or later, then you may take advantage of analytic functions, which would really help here.
– Tim Biegeleisen
Nov 22 at 14:05
This looks like a gaps and islands problem to me, or at least a variant of it. If you are using MySQL 8+ or later, then you may take advantage of analytic functions, which would really help here.
– Tim Biegeleisen
Nov 22 at 14:05
MySQL 5.7. There is no pk in my table
– Gongo82
Nov 22 at 14:06
MySQL 5.7. There is no pk in my table
– Gongo82
Nov 22 at 14:06
2
2
If you have no PK, then you have no table.
– Strawberry
Nov 22 at 14:09
If you have no PK, then you have no table.
– Strawberry
Nov 22 at 14:09
1
1
@Arth It depends on the engine (I think)
– Strawberry
Nov 22 at 18:21
@Arth It depends on the engine (I think)
– Strawberry
Nov 22 at 18:21
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432638%2fmysql-group-rows-by-progressive-date-in-consecutive-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is your MySQL server version ? Is there any Primary key in your table ? Without that we really cant define "order", as data is unordered
– Madhur Bhaiya
Nov 22 at 14:01
This looks like a gaps and islands problem to me, or at least a variant of it. If you are using MySQL 8+ or later, then you may take advantage of analytic functions, which would really help here.
– Tim Biegeleisen
Nov 22 at 14:05
MySQL 5.7. There is no pk in my table
– Gongo82
Nov 22 at 14:06
2
If you have no PK, then you have no table.
– Strawberry
Nov 22 at 14:09
1
@Arth It depends on the engine (I think)
– Strawberry
Nov 22 at 18:21