MySQL group rows by progressive date in consecutive rows












2















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?










share|improve this question


















  • 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 '18 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 '18 at 14:05











  • MySQL 5.7. There is no pk in my table

    – Gongo82
    Nov 22 '18 at 14:06






  • 2





    If you have no PK, then you have no table.

    – Strawberry
    Nov 22 '18 at 14:09






  • 1





    @Arth It depends on the engine (I think)

    – Strawberry
    Nov 22 '18 at 18:21
















2















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?










share|improve this question


















  • 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 '18 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 '18 at 14:05











  • MySQL 5.7. There is no pk in my table

    – Gongo82
    Nov 22 '18 at 14:06






  • 2





    If you have no PK, then you have no table.

    – Strawberry
    Nov 22 '18 at 14:09






  • 1





    @Arth It depends on the engine (I think)

    – Strawberry
    Nov 22 '18 at 18:21














2












2








2


0






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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 14:01









Gongo82Gongo82

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 '18 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 '18 at 14:05











  • MySQL 5.7. There is no pk in my table

    – Gongo82
    Nov 22 '18 at 14:06






  • 2





    If you have no PK, then you have no table.

    – Strawberry
    Nov 22 '18 at 14:09






  • 1





    @Arth It depends on the engine (I think)

    – Strawberry
    Nov 22 '18 at 18:21














  • 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 '18 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 '18 at 14:05











  • MySQL 5.7. There is no pk in my table

    – Gongo82
    Nov 22 '18 at 14:06






  • 2





    If you have no PK, then you have no table.

    – Strawberry
    Nov 22 '18 at 14:09






  • 1





    @Arth It depends on the engine (I think)

    – Strawberry
    Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 14:05













MySQL 5.7. There is no pk in my table

– Gongo82
Nov 22 '18 at 14:06





MySQL 5.7. There is no pk in my table

– Gongo82
Nov 22 '18 at 14:06




2




2





If you have no PK, then you have no table.

– Strawberry
Nov 22 '18 at 14:09





If you have no PK, then you have no table.

– Strawberry
Nov 22 '18 at 14:09




1




1





@Arth It depends on the engine (I think)

– Strawberry
Nov 22 '18 at 18:21





@Arth It depends on the engine (I think)

– Strawberry
Nov 22 '18 at 18:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?