Group by range of DATETIME - MySql





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-5















Well, I have a table with some columns that some of these are repeated, but the time is not exactly the same, I need to do a group by that has a range of 2 hours.



Notice that all the fields marked in black repeat, but the datetime field may differ by a few minutes, so I need to make a group by joining everyone within that two-hour interval.



enter image description here



It would be something like: SELECT * FROM tabela GROUP BY sinistro, prefixo, data AND horaIfull_oco range 2h



And also in the result it is necessary that the columns come with all their ids if possible, eg: id1, id2 etc etc



He would join all those who had a maximum of two hours apart.
I have already researched and found nothing, if anyone knows how to do.










share|improve this question




















  • 2





    Lets say there are two time values: 02:01:00 and 04:00:00. Different between them is less than two hours. So both will be grouped together; or they will be under the 02:00:00 and 04:00:00 groups respectively ?

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:04











  • @MadhurBhaiya will be grouped in a single result, no matter which of them appears, I just need them to be grouped and if possible, in return come the id of each one, example if grouped 2 records, have there: id1, id2 ...

    – Woton Sampaio
    Nov 17 '18 at 5:07






  • 2





    @WotonSampaio Your two responses above are contradictory, if you are struggling to explain in English.. explain in data! Provide sample data and a desired response for a range of rows with times within 2 hours and not of each other. I can see 3 possible interpretations of your question; groups start at arbitrary times 2 hrs apart (2,4,6,8); groups start at the earliest (previously ungrouped) time and extend for 2 hours; or all chains of rows within 2 hours of each other form one group (rows at 1,2,3,4,5,6, 9,10,11,12 form two groups (1,2,3,4,5,6) and (9,10,11,12)).

    – Arth
    Nov 21 '18 at 15:56








  • 1





    @WotonSampaio perhaps a GROUP BY HOUR(horaIfull_oco) be enough? Although as i understand you have 12 2-hour periods in a day so every listing in the database for that day would have to belong in one of those 12 2-hour periods. A query like that would definitely need IF statement or CASE .. WHEN.

    – Peter Darmis
    Nov 26 '18 at 9:44








  • 1





    @WotonSampaio continuing from last comment i think you need a CASE .. WHEN statement used in your GROUP BY. I will post an example answer later on.

    – Peter Darmis
    Nov 26 '18 at 9:50


















-5















Well, I have a table with some columns that some of these are repeated, but the time is not exactly the same, I need to do a group by that has a range of 2 hours.



Notice that all the fields marked in black repeat, but the datetime field may differ by a few minutes, so I need to make a group by joining everyone within that two-hour interval.



enter image description here



It would be something like: SELECT * FROM tabela GROUP BY sinistro, prefixo, data AND horaIfull_oco range 2h



And also in the result it is necessary that the columns come with all their ids if possible, eg: id1, id2 etc etc



He would join all those who had a maximum of two hours apart.
I have already researched and found nothing, if anyone knows how to do.










share|improve this question




















  • 2





    Lets say there are two time values: 02:01:00 and 04:00:00. Different between them is less than two hours. So both will be grouped together; or they will be under the 02:00:00 and 04:00:00 groups respectively ?

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:04











  • @MadhurBhaiya will be grouped in a single result, no matter which of them appears, I just need them to be grouped and if possible, in return come the id of each one, example if grouped 2 records, have there: id1, id2 ...

    – Woton Sampaio
    Nov 17 '18 at 5:07






  • 2





    @WotonSampaio Your two responses above are contradictory, if you are struggling to explain in English.. explain in data! Provide sample data and a desired response for a range of rows with times within 2 hours and not of each other. I can see 3 possible interpretations of your question; groups start at arbitrary times 2 hrs apart (2,4,6,8); groups start at the earliest (previously ungrouped) time and extend for 2 hours; or all chains of rows within 2 hours of each other form one group (rows at 1,2,3,4,5,6, 9,10,11,12 form two groups (1,2,3,4,5,6) and (9,10,11,12)).

    – Arth
    Nov 21 '18 at 15:56








  • 1





    @WotonSampaio perhaps a GROUP BY HOUR(horaIfull_oco) be enough? Although as i understand you have 12 2-hour periods in a day so every listing in the database for that day would have to belong in one of those 12 2-hour periods. A query like that would definitely need IF statement or CASE .. WHEN.

    – Peter Darmis
    Nov 26 '18 at 9:44








  • 1





    @WotonSampaio continuing from last comment i think you need a CASE .. WHEN statement used in your GROUP BY. I will post an example answer later on.

    – Peter Darmis
    Nov 26 '18 at 9:50














-5












-5








-5








Well, I have a table with some columns that some of these are repeated, but the time is not exactly the same, I need to do a group by that has a range of 2 hours.



Notice that all the fields marked in black repeat, but the datetime field may differ by a few minutes, so I need to make a group by joining everyone within that two-hour interval.



enter image description here



It would be something like: SELECT * FROM tabela GROUP BY sinistro, prefixo, data AND horaIfull_oco range 2h



And also in the result it is necessary that the columns come with all their ids if possible, eg: id1, id2 etc etc



He would join all those who had a maximum of two hours apart.
I have already researched and found nothing, if anyone knows how to do.










share|improve this question
















Well, I have a table with some columns that some of these are repeated, but the time is not exactly the same, I need to do a group by that has a range of 2 hours.



Notice that all the fields marked in black repeat, but the datetime field may differ by a few minutes, so I need to make a group by joining everyone within that two-hour interval.



enter image description here



It would be something like: SELECT * FROM tabela GROUP BY sinistro, prefixo, data AND horaIfull_oco range 2h



And also in the result it is necessary that the columns come with all their ids if possible, eg: id1, id2 etc etc



He would join all those who had a maximum of two hours apart.
I have already researched and found nothing, if anyone knows how to do.







mysql database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 9:53







Woton Sampaio

















asked Nov 17 '18 at 4:56









Woton SampaioWoton Sampaio

163216




163216








  • 2





    Lets say there are two time values: 02:01:00 and 04:00:00. Different between them is less than two hours. So both will be grouped together; or they will be under the 02:00:00 and 04:00:00 groups respectively ?

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:04











  • @MadhurBhaiya will be grouped in a single result, no matter which of them appears, I just need them to be grouped and if possible, in return come the id of each one, example if grouped 2 records, have there: id1, id2 ...

    – Woton Sampaio
    Nov 17 '18 at 5:07






  • 2





    @WotonSampaio Your two responses above are contradictory, if you are struggling to explain in English.. explain in data! Provide sample data and a desired response for a range of rows with times within 2 hours and not of each other. I can see 3 possible interpretations of your question; groups start at arbitrary times 2 hrs apart (2,4,6,8); groups start at the earliest (previously ungrouped) time and extend for 2 hours; or all chains of rows within 2 hours of each other form one group (rows at 1,2,3,4,5,6, 9,10,11,12 form two groups (1,2,3,4,5,6) and (9,10,11,12)).

    – Arth
    Nov 21 '18 at 15:56








  • 1





    @WotonSampaio perhaps a GROUP BY HOUR(horaIfull_oco) be enough? Although as i understand you have 12 2-hour periods in a day so every listing in the database for that day would have to belong in one of those 12 2-hour periods. A query like that would definitely need IF statement or CASE .. WHEN.

    – Peter Darmis
    Nov 26 '18 at 9:44








  • 1





    @WotonSampaio continuing from last comment i think you need a CASE .. WHEN statement used in your GROUP BY. I will post an example answer later on.

    – Peter Darmis
    Nov 26 '18 at 9:50














  • 2





    Lets say there are two time values: 02:01:00 and 04:00:00. Different between them is less than two hours. So both will be grouped together; or they will be under the 02:00:00 and 04:00:00 groups respectively ?

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:04











  • @MadhurBhaiya will be grouped in a single result, no matter which of them appears, I just need them to be grouped and if possible, in return come the id of each one, example if grouped 2 records, have there: id1, id2 ...

    – Woton Sampaio
    Nov 17 '18 at 5:07






  • 2





    @WotonSampaio Your two responses above are contradictory, if you are struggling to explain in English.. explain in data! Provide sample data and a desired response for a range of rows with times within 2 hours and not of each other. I can see 3 possible interpretations of your question; groups start at arbitrary times 2 hrs apart (2,4,6,8); groups start at the earliest (previously ungrouped) time and extend for 2 hours; or all chains of rows within 2 hours of each other form one group (rows at 1,2,3,4,5,6, 9,10,11,12 form two groups (1,2,3,4,5,6) and (9,10,11,12)).

    – Arth
    Nov 21 '18 at 15:56








  • 1





    @WotonSampaio perhaps a GROUP BY HOUR(horaIfull_oco) be enough? Although as i understand you have 12 2-hour periods in a day so every listing in the database for that day would have to belong in one of those 12 2-hour periods. A query like that would definitely need IF statement or CASE .. WHEN.

    – Peter Darmis
    Nov 26 '18 at 9:44








  • 1





    @WotonSampaio continuing from last comment i think you need a CASE .. WHEN statement used in your GROUP BY. I will post an example answer later on.

    – Peter Darmis
    Nov 26 '18 at 9:50








2




2





Lets say there are two time values: 02:01:00 and 04:00:00. Different between them is less than two hours. So both will be grouped together; or they will be under the 02:00:00 and 04:00:00 groups respectively ?

– Chowkidar Madhur Bhaiya
Nov 17 '18 at 5:04





Lets say there are two time values: 02:01:00 and 04:00:00. Different between them is less than two hours. So both will be grouped together; or they will be under the 02:00:00 and 04:00:00 groups respectively ?

– Chowkidar Madhur Bhaiya
Nov 17 '18 at 5:04













@MadhurBhaiya will be grouped in a single result, no matter which of them appears, I just need them to be grouped and if possible, in return come the id of each one, example if grouped 2 records, have there: id1, id2 ...

– Woton Sampaio
Nov 17 '18 at 5:07





@MadhurBhaiya will be grouped in a single result, no matter which of them appears, I just need them to be grouped and if possible, in return come the id of each one, example if grouped 2 records, have there: id1, id2 ...

– Woton Sampaio
Nov 17 '18 at 5:07




2




2





@WotonSampaio Your two responses above are contradictory, if you are struggling to explain in English.. explain in data! Provide sample data and a desired response for a range of rows with times within 2 hours and not of each other. I can see 3 possible interpretations of your question; groups start at arbitrary times 2 hrs apart (2,4,6,8); groups start at the earliest (previously ungrouped) time and extend for 2 hours; or all chains of rows within 2 hours of each other form one group (rows at 1,2,3,4,5,6, 9,10,11,12 form two groups (1,2,3,4,5,6) and (9,10,11,12)).

– Arth
Nov 21 '18 at 15:56







@WotonSampaio Your two responses above are contradictory, if you are struggling to explain in English.. explain in data! Provide sample data and a desired response for a range of rows with times within 2 hours and not of each other. I can see 3 possible interpretations of your question; groups start at arbitrary times 2 hrs apart (2,4,6,8); groups start at the earliest (previously ungrouped) time and extend for 2 hours; or all chains of rows within 2 hours of each other form one group (rows at 1,2,3,4,5,6, 9,10,11,12 form two groups (1,2,3,4,5,6) and (9,10,11,12)).

– Arth
Nov 21 '18 at 15:56






1




1





@WotonSampaio perhaps a GROUP BY HOUR(horaIfull_oco) be enough? Although as i understand you have 12 2-hour periods in a day so every listing in the database for that day would have to belong in one of those 12 2-hour periods. A query like that would definitely need IF statement or CASE .. WHEN.

– Peter Darmis
Nov 26 '18 at 9:44







@WotonSampaio perhaps a GROUP BY HOUR(horaIfull_oco) be enough? Although as i understand you have 12 2-hour periods in a day so every listing in the database for that day would have to belong in one of those 12 2-hour periods. A query like that would definitely need IF statement or CASE .. WHEN.

– Peter Darmis
Nov 26 '18 at 9:44






1




1





@WotonSampaio continuing from last comment i think you need a CASE .. WHEN statement used in your GROUP BY. I will post an example answer later on.

– Peter Darmis
Nov 26 '18 at 9:50





@WotonSampaio continuing from last comment i think you need a CASE .. WHEN statement used in your GROUP BY. I will post an example answer later on.

– Peter Darmis
Nov 26 '18 at 9:50












3 Answers
3






active

oldest

votes


















1














You can select floor(hour / 2) which will tell you which two hour period it belongs in and group by that along with the date:



SELECT *,
group_concat(id_oco) as grouped_ids,
floor(HOUR(horaIfull_oco) / 2) AS two_hour
FROM tabela
GROUP BY inclusao_oco, sinistro, prefixo, data, DATE(horaIfull_oco), two_hour





share|improve this answer





















  • 3





    Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

    – Shidersz
    Nov 17 '18 at 5:19






  • 2





    @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

    – fyrye
    Nov 17 '18 at 5:19






  • 1





    @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:22






  • 1





    @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

    – fyrye
    Nov 17 '18 at 5:25








  • 1





    @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

    – billynoah
    Nov 17 '18 at 5:26



















1














GROUP BY FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)


(7200 is 2 hours.)



If you want to get the base time for each group:



FROM_UNIXTIME(
FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
, 7200)


This technique can be adapted to any regular time interval by changing 7200.






share|improve this answer
























  • Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

    – Woton Sampaio
    Nov 20 '18 at 2:57








  • 2





    @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

    – Rick James
    Nov 20 '18 at 3:05











  • I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

    – Woton Sampaio
    Nov 20 '18 at 3:22



















1














Assuming a database table having a table with the following schema:



CREATE TABLE t1 (
c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
c2 VARCHAR(100),
c3 VARCHAR(100),
c4 DATE,
c5 TIME,
c6 DATETIME);

INSERT INTO t1 (c1,c2,c3,c4,c5,c6)
VALUES
(1,'DDA1123_12112018','55307','2018-12-11','00:43:00', '2018-12-11 00:43:00'),
(2,'DDA1123_12112018','55307','2018-12-11','00:07:00', '2018-12-11 00:07:00'),
(3,'DDA1124_12112018','55308','2018-12-11','03:07:00', '2018-12-11 03:07:00'),
(4,'DDA1124_12112018','55308','2018-12-11','04:07:00', '2018-12-11 04:07:00'),
(5,'DDA1124_12112018','55308','2018-12-11','07:07:00', '2018-12-11 07:07:00'),
(6,'DDA1125_12112018','55309','2018-12-11','06:07:00', '2018-12-11 06:07:00'),
(7,'DDA1125_12112018','55309','2018-12-11','07:57:00', '2018-12-11 07:57:00'),
(8,'DDA1126_12112018','55310','2018-12-11','08:07:00', '2018-12-11 08:07:00'),
(9,'DDA1127_12112018','55311','2018-12-11','12:07:00', '2018-12-11 12:07:00'),
(10,'DDA1127_12112018','55311','2018-12-11','14:07:00', '2018-12-11 14:07:00'),
(11,'DDA1127_12112018','55311','2018-12-11','17:07:00', '2018-12-11 17:07:00');


In that case you could use CASE ... WHEN in GROUP BY like in this example query.



SELECT c1,c2,c3,c4,c5,c6,
CASE TRUE
WHEN HOUR(c6) BETWEEN 0 AND 2 THEN 0
WHEN HOUR(c6) BETWEEN 2 AND 4 THEN 2
WHEN HOUR(c6) BETWEEN 4 AND 6 THEN 4
WHEN HOUR(c6) BETWEEN 6 AND 8 THEN 6
WHEN HOUR(c6) BETWEEN 8 AND 10 THEN 8
WHEN HOUR(c6) BETWEEN 10 AND 12 THEN 10
WHEN HOUR(c6) BETWEEN 12 AND 14 THEN 12
WHEN HOUR(c6) BETWEEN 14 AND 16 THEN 14
WHEN HOUR(c6) BETWEEN 16 AND 18 THEN 16
WHEN HOUR(c6) BETWEEN 18 AND 20 THEN 18
WHEN HOUR(c6) BETWEEN 20 AND 22 THEN 20
WHEN HOUR(c6) BETWEEN 22 AND 24 THEN 22
END AS c7 FROM t1
GROUP BY c7;


You can view the results of the above example in SQL Fiddle.



EDIT



For better results the above query could be re-written:



SELECT c1,c2,c3,c4,c5,c6,
CASE TRUE
WHEN HOUR(c6) BETWEEN 0 AND 1 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 0
WHEN HOUR(c6) BETWEEN 2 AND 3 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 2
WHEN HOUR(c6) BETWEEN 4 AND 5 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 4
WHEN HOUR(c6) BETWEEN 6 AND 7 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 6
WHEN HOUR(c6) BETWEEN 8 AND 9 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 8
WHEN HOUR(c6) BETWEEN 10 AND 11 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 10
WHEN HOUR(c6) BETWEEN 12 AND 13 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 12
WHEN HOUR(c6) BETWEEN 14 AND 15 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 14
WHEN HOUR(c6) BETWEEN 16 AND 17 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 16
WHEN HOUR(c6) BETWEEN 18 AND 19 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 18
WHEN HOUR(c6) BETWEEN 20 AND 21 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 20
WHEN HOUR(c6) BETWEEN 22 AND 23 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 22
END AS c7 FROM t1
GROUP BY c7;


You can view the results of the edited example in this SQL Fiddle.






share|improve this answer


























    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%2f53348367%2fgroup-by-range-of-datetime-mysql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can select floor(hour / 2) which will tell you which two hour period it belongs in and group by that along with the date:



    SELECT *,
    group_concat(id_oco) as grouped_ids,
    floor(HOUR(horaIfull_oco) / 2) AS two_hour
    FROM tabela
    GROUP BY inclusao_oco, sinistro, prefixo, data, DATE(horaIfull_oco), two_hour





    share|improve this answer





















    • 3





      Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

      – Shidersz
      Nov 17 '18 at 5:19






    • 2





      @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

      – fyrye
      Nov 17 '18 at 5:19






    • 1





      @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

      – Chowkidar Madhur Bhaiya
      Nov 17 '18 at 5:22






    • 1





      @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

      – fyrye
      Nov 17 '18 at 5:25








    • 1





      @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

      – billynoah
      Nov 17 '18 at 5:26
















    1














    You can select floor(hour / 2) which will tell you which two hour period it belongs in and group by that along with the date:



    SELECT *,
    group_concat(id_oco) as grouped_ids,
    floor(HOUR(horaIfull_oco) / 2) AS two_hour
    FROM tabela
    GROUP BY inclusao_oco, sinistro, prefixo, data, DATE(horaIfull_oco), two_hour





    share|improve this answer





















    • 3





      Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

      – Shidersz
      Nov 17 '18 at 5:19






    • 2





      @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

      – fyrye
      Nov 17 '18 at 5:19






    • 1





      @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

      – Chowkidar Madhur Bhaiya
      Nov 17 '18 at 5:22






    • 1





      @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

      – fyrye
      Nov 17 '18 at 5:25








    • 1





      @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

      – billynoah
      Nov 17 '18 at 5:26














    1












    1








    1







    You can select floor(hour / 2) which will tell you which two hour period it belongs in and group by that along with the date:



    SELECT *,
    group_concat(id_oco) as grouped_ids,
    floor(HOUR(horaIfull_oco) / 2) AS two_hour
    FROM tabela
    GROUP BY inclusao_oco, sinistro, prefixo, data, DATE(horaIfull_oco), two_hour





    share|improve this answer















    You can select floor(hour / 2) which will tell you which two hour period it belongs in and group by that along with the date:



    SELECT *,
    group_concat(id_oco) as grouped_ids,
    floor(HOUR(horaIfull_oco) / 2) AS two_hour
    FROM tabela
    GROUP BY inclusao_oco, sinistro, prefixo, data, DATE(horaIfull_oco), two_hour






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 17 '18 at 5:14

























    answered Nov 17 '18 at 5:09









    billynoahbillynoah

    11.1k64468




    11.1k64468








    • 3





      Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

      – Shidersz
      Nov 17 '18 at 5:19






    • 2





      @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

      – fyrye
      Nov 17 '18 at 5:19






    • 1





      @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

      – Chowkidar Madhur Bhaiya
      Nov 17 '18 at 5:22






    • 1





      @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

      – fyrye
      Nov 17 '18 at 5:25








    • 1





      @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

      – billynoah
      Nov 17 '18 at 5:26














    • 3





      Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

      – Shidersz
      Nov 17 '18 at 5:19






    • 2





      @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

      – fyrye
      Nov 17 '18 at 5:19






    • 1





      @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

      – Chowkidar Madhur Bhaiya
      Nov 17 '18 at 5:22






    • 1





      @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

      – fyrye
      Nov 17 '18 at 5:25








    • 1





      @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

      – billynoah
      Nov 17 '18 at 5:26








    3




    3





    Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

    – Shidersz
    Nov 17 '18 at 5:19





    Note that SELECT * under a GROUP BY clause is not recommended, this can generate conflicts with engines that have the option ONLY_FULL_GROUP_BY.

    – Shidersz
    Nov 17 '18 at 5:19




    2




    2





    @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

    – fyrye
    Nov 17 '18 at 5:19





    @WotonSampaio keep in mind that using GROUP BY without an aggregate function, MySQL can pick any value within the grouped records. For example id_oco may be 1 on one query, and 5 on another, unless you explicitly state MAX(id_oco).

    – fyrye
    Nov 17 '18 at 5:19




    1




    1





    @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:22





    @WotonSampaio It will matter. Just try running the code in other RDBMS, or the latest versions of MySQL (5.7 and above); it will fail. The current answers utilizes a (mis)feature of older versions of MySQL, which were lenient.

    – Chowkidar Madhur Bhaiya
    Nov 17 '18 at 5:22




    1




    1





    @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

    – fyrye
    Nov 17 '18 at 5:25







    @WotonSampaio if the only desired value is from the SELECT GROUP_CONCAT FROM that would qualify as your aggregate function. However in your question, you did not specify an end result of the data to be retrieved and stated SELECT *, so it appears that you were/are looking for every column from that grouping without an aggregate. Which can and will retrieve random records from those columns. Also keep in mind GROUP_CONCAT has a character limit of 1024 by default.

    – fyrye
    Nov 17 '18 at 5:25






    1




    1





    @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

    – billynoah
    Nov 17 '18 at 5:26





    @WotonSampaio - The points being made here about this select are valid. You will be ok as long as you (a) understand that the value returned on any non-aggregated column will be arbitrary and (b) are ok with that know how to turn off full group by mode. I've been in situations where it's actually beneficial to see some arbitrary values of the grouped data, but you really need to understand that it's happening when you view your results

    – billynoah
    Nov 17 '18 at 5:26













    1














    GROUP BY FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)


    (7200 is 2 hours.)



    If you want to get the base time for each group:



    FROM_UNIXTIME(
    FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
    , 7200)


    This technique can be adapted to any regular time interval by changing 7200.






    share|improve this answer
























    • Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

      – Woton Sampaio
      Nov 20 '18 at 2:57








    • 2





      @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

      – Rick James
      Nov 20 '18 at 3:05











    • I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

      – Woton Sampaio
      Nov 20 '18 at 3:22
















    1














    GROUP BY FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)


    (7200 is 2 hours.)



    If you want to get the base time for each group:



    FROM_UNIXTIME(
    FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
    , 7200)


    This technique can be adapted to any regular time interval by changing 7200.






    share|improve this answer
























    • Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

      – Woton Sampaio
      Nov 20 '18 at 2:57








    • 2





      @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

      – Rick James
      Nov 20 '18 at 3:05











    • I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

      – Woton Sampaio
      Nov 20 '18 at 3:22














    1












    1








    1







    GROUP BY FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)


    (7200 is 2 hours.)



    If you want to get the base time for each group:



    FROM_UNIXTIME(
    FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
    , 7200)


    This technique can be adapted to any regular time interval by changing 7200.






    share|improve this answer













    GROUP BY FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)


    (7200 is 2 hours.)



    If you want to get the base time for each group:



    FROM_UNIXTIME(
    FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
    , 7200)


    This technique can be adapted to any regular time interval by changing 7200.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 2:39









    Rick JamesRick James

    71.4k567106




    71.4k567106













    • Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

      – Woton Sampaio
      Nov 20 '18 at 2:57








    • 2





      @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

      – Rick James
      Nov 20 '18 at 3:05











    • I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

      – Woton Sampaio
      Nov 20 '18 at 3:22



















    • Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

      – Woton Sampaio
      Nov 20 '18 at 2:57








    • 2





      @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

      – Rick James
      Nov 20 '18 at 3:05











    • I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

      – Woton Sampaio
      Nov 20 '18 at 3:22

















    Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

    – Woton Sampaio
    Nov 20 '18 at 2:57







    Considering that you have 2 records, one from 2018-11-19 23:50:00 and one from 2018-11-20 00:10:00 they do not join with your query, so far the only query I saw that really solved my problem is the one I answered below

    – Woton Sampaio
    Nov 20 '18 at 2:57






    2




    2





    @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

    – Rick James
    Nov 20 '18 at 3:05





    @WotonSampaio - that is a much tougher problem. What if there is a reading every 119 minutes. Each consecutive pair could be combined, but which pairs should be combined. Or every 11 minutes. I suggest that even 2-hour boundaries is as good as any other algorithm.

    – Rick James
    Nov 20 '18 at 3:05













    I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

    – Woton Sampaio
    Nov 20 '18 at 3:22





    I do not understand very well what I mean (my English is a bit bad), and I also do not know much about database optimization, if you can leave a better explanation I thank you :)

    – Woton Sampaio
    Nov 20 '18 at 3:22











    1














    Assuming a database table having a table with the following schema:



    CREATE TABLE t1 (
    c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    c2 VARCHAR(100),
    c3 VARCHAR(100),
    c4 DATE,
    c5 TIME,
    c6 DATETIME);

    INSERT INTO t1 (c1,c2,c3,c4,c5,c6)
    VALUES
    (1,'DDA1123_12112018','55307','2018-12-11','00:43:00', '2018-12-11 00:43:00'),
    (2,'DDA1123_12112018','55307','2018-12-11','00:07:00', '2018-12-11 00:07:00'),
    (3,'DDA1124_12112018','55308','2018-12-11','03:07:00', '2018-12-11 03:07:00'),
    (4,'DDA1124_12112018','55308','2018-12-11','04:07:00', '2018-12-11 04:07:00'),
    (5,'DDA1124_12112018','55308','2018-12-11','07:07:00', '2018-12-11 07:07:00'),
    (6,'DDA1125_12112018','55309','2018-12-11','06:07:00', '2018-12-11 06:07:00'),
    (7,'DDA1125_12112018','55309','2018-12-11','07:57:00', '2018-12-11 07:57:00'),
    (8,'DDA1126_12112018','55310','2018-12-11','08:07:00', '2018-12-11 08:07:00'),
    (9,'DDA1127_12112018','55311','2018-12-11','12:07:00', '2018-12-11 12:07:00'),
    (10,'DDA1127_12112018','55311','2018-12-11','14:07:00', '2018-12-11 14:07:00'),
    (11,'DDA1127_12112018','55311','2018-12-11','17:07:00', '2018-12-11 17:07:00');


    In that case you could use CASE ... WHEN in GROUP BY like in this example query.



    SELECT c1,c2,c3,c4,c5,c6,
    CASE TRUE
    WHEN HOUR(c6) BETWEEN 0 AND 2 THEN 0
    WHEN HOUR(c6) BETWEEN 2 AND 4 THEN 2
    WHEN HOUR(c6) BETWEEN 4 AND 6 THEN 4
    WHEN HOUR(c6) BETWEEN 6 AND 8 THEN 6
    WHEN HOUR(c6) BETWEEN 8 AND 10 THEN 8
    WHEN HOUR(c6) BETWEEN 10 AND 12 THEN 10
    WHEN HOUR(c6) BETWEEN 12 AND 14 THEN 12
    WHEN HOUR(c6) BETWEEN 14 AND 16 THEN 14
    WHEN HOUR(c6) BETWEEN 16 AND 18 THEN 16
    WHEN HOUR(c6) BETWEEN 18 AND 20 THEN 18
    WHEN HOUR(c6) BETWEEN 20 AND 22 THEN 20
    WHEN HOUR(c6) BETWEEN 22 AND 24 THEN 22
    END AS c7 FROM t1
    GROUP BY c7;


    You can view the results of the above example in SQL Fiddle.



    EDIT



    For better results the above query could be re-written:



    SELECT c1,c2,c3,c4,c5,c6,
    CASE TRUE
    WHEN HOUR(c6) BETWEEN 0 AND 1 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 0
    WHEN HOUR(c6) BETWEEN 2 AND 3 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 2
    WHEN HOUR(c6) BETWEEN 4 AND 5 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 4
    WHEN HOUR(c6) BETWEEN 6 AND 7 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 6
    WHEN HOUR(c6) BETWEEN 8 AND 9 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 8
    WHEN HOUR(c6) BETWEEN 10 AND 11 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 10
    WHEN HOUR(c6) BETWEEN 12 AND 13 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 12
    WHEN HOUR(c6) BETWEEN 14 AND 15 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 14
    WHEN HOUR(c6) BETWEEN 16 AND 17 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 16
    WHEN HOUR(c6) BETWEEN 18 AND 19 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 18
    WHEN HOUR(c6) BETWEEN 20 AND 21 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 20
    WHEN HOUR(c6) BETWEEN 22 AND 23 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 22
    END AS c7 FROM t1
    GROUP BY c7;


    You can view the results of the edited example in this SQL Fiddle.






    share|improve this answer






























      1














      Assuming a database table having a table with the following schema:



      CREATE TABLE t1 (
      c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      c2 VARCHAR(100),
      c3 VARCHAR(100),
      c4 DATE,
      c5 TIME,
      c6 DATETIME);

      INSERT INTO t1 (c1,c2,c3,c4,c5,c6)
      VALUES
      (1,'DDA1123_12112018','55307','2018-12-11','00:43:00', '2018-12-11 00:43:00'),
      (2,'DDA1123_12112018','55307','2018-12-11','00:07:00', '2018-12-11 00:07:00'),
      (3,'DDA1124_12112018','55308','2018-12-11','03:07:00', '2018-12-11 03:07:00'),
      (4,'DDA1124_12112018','55308','2018-12-11','04:07:00', '2018-12-11 04:07:00'),
      (5,'DDA1124_12112018','55308','2018-12-11','07:07:00', '2018-12-11 07:07:00'),
      (6,'DDA1125_12112018','55309','2018-12-11','06:07:00', '2018-12-11 06:07:00'),
      (7,'DDA1125_12112018','55309','2018-12-11','07:57:00', '2018-12-11 07:57:00'),
      (8,'DDA1126_12112018','55310','2018-12-11','08:07:00', '2018-12-11 08:07:00'),
      (9,'DDA1127_12112018','55311','2018-12-11','12:07:00', '2018-12-11 12:07:00'),
      (10,'DDA1127_12112018','55311','2018-12-11','14:07:00', '2018-12-11 14:07:00'),
      (11,'DDA1127_12112018','55311','2018-12-11','17:07:00', '2018-12-11 17:07:00');


      In that case you could use CASE ... WHEN in GROUP BY like in this example query.



      SELECT c1,c2,c3,c4,c5,c6,
      CASE TRUE
      WHEN HOUR(c6) BETWEEN 0 AND 2 THEN 0
      WHEN HOUR(c6) BETWEEN 2 AND 4 THEN 2
      WHEN HOUR(c6) BETWEEN 4 AND 6 THEN 4
      WHEN HOUR(c6) BETWEEN 6 AND 8 THEN 6
      WHEN HOUR(c6) BETWEEN 8 AND 10 THEN 8
      WHEN HOUR(c6) BETWEEN 10 AND 12 THEN 10
      WHEN HOUR(c6) BETWEEN 12 AND 14 THEN 12
      WHEN HOUR(c6) BETWEEN 14 AND 16 THEN 14
      WHEN HOUR(c6) BETWEEN 16 AND 18 THEN 16
      WHEN HOUR(c6) BETWEEN 18 AND 20 THEN 18
      WHEN HOUR(c6) BETWEEN 20 AND 22 THEN 20
      WHEN HOUR(c6) BETWEEN 22 AND 24 THEN 22
      END AS c7 FROM t1
      GROUP BY c7;


      You can view the results of the above example in SQL Fiddle.



      EDIT



      For better results the above query could be re-written:



      SELECT c1,c2,c3,c4,c5,c6,
      CASE TRUE
      WHEN HOUR(c6) BETWEEN 0 AND 1 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 0
      WHEN HOUR(c6) BETWEEN 2 AND 3 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 2
      WHEN HOUR(c6) BETWEEN 4 AND 5 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 4
      WHEN HOUR(c6) BETWEEN 6 AND 7 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 6
      WHEN HOUR(c6) BETWEEN 8 AND 9 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 8
      WHEN HOUR(c6) BETWEEN 10 AND 11 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 10
      WHEN HOUR(c6) BETWEEN 12 AND 13 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 12
      WHEN HOUR(c6) BETWEEN 14 AND 15 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 14
      WHEN HOUR(c6) BETWEEN 16 AND 17 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 16
      WHEN HOUR(c6) BETWEEN 18 AND 19 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 18
      WHEN HOUR(c6) BETWEEN 20 AND 21 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 20
      WHEN HOUR(c6) BETWEEN 22 AND 23 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 22
      END AS c7 FROM t1
      GROUP BY c7;


      You can view the results of the edited example in this SQL Fiddle.






      share|improve this answer




























        1












        1








        1







        Assuming a database table having a table with the following schema:



        CREATE TABLE t1 (
        c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        c2 VARCHAR(100),
        c3 VARCHAR(100),
        c4 DATE,
        c5 TIME,
        c6 DATETIME);

        INSERT INTO t1 (c1,c2,c3,c4,c5,c6)
        VALUES
        (1,'DDA1123_12112018','55307','2018-12-11','00:43:00', '2018-12-11 00:43:00'),
        (2,'DDA1123_12112018','55307','2018-12-11','00:07:00', '2018-12-11 00:07:00'),
        (3,'DDA1124_12112018','55308','2018-12-11','03:07:00', '2018-12-11 03:07:00'),
        (4,'DDA1124_12112018','55308','2018-12-11','04:07:00', '2018-12-11 04:07:00'),
        (5,'DDA1124_12112018','55308','2018-12-11','07:07:00', '2018-12-11 07:07:00'),
        (6,'DDA1125_12112018','55309','2018-12-11','06:07:00', '2018-12-11 06:07:00'),
        (7,'DDA1125_12112018','55309','2018-12-11','07:57:00', '2018-12-11 07:57:00'),
        (8,'DDA1126_12112018','55310','2018-12-11','08:07:00', '2018-12-11 08:07:00'),
        (9,'DDA1127_12112018','55311','2018-12-11','12:07:00', '2018-12-11 12:07:00'),
        (10,'DDA1127_12112018','55311','2018-12-11','14:07:00', '2018-12-11 14:07:00'),
        (11,'DDA1127_12112018','55311','2018-12-11','17:07:00', '2018-12-11 17:07:00');


        In that case you could use CASE ... WHEN in GROUP BY like in this example query.



        SELECT c1,c2,c3,c4,c5,c6,
        CASE TRUE
        WHEN HOUR(c6) BETWEEN 0 AND 2 THEN 0
        WHEN HOUR(c6) BETWEEN 2 AND 4 THEN 2
        WHEN HOUR(c6) BETWEEN 4 AND 6 THEN 4
        WHEN HOUR(c6) BETWEEN 6 AND 8 THEN 6
        WHEN HOUR(c6) BETWEEN 8 AND 10 THEN 8
        WHEN HOUR(c6) BETWEEN 10 AND 12 THEN 10
        WHEN HOUR(c6) BETWEEN 12 AND 14 THEN 12
        WHEN HOUR(c6) BETWEEN 14 AND 16 THEN 14
        WHEN HOUR(c6) BETWEEN 16 AND 18 THEN 16
        WHEN HOUR(c6) BETWEEN 18 AND 20 THEN 18
        WHEN HOUR(c6) BETWEEN 20 AND 22 THEN 20
        WHEN HOUR(c6) BETWEEN 22 AND 24 THEN 22
        END AS c7 FROM t1
        GROUP BY c7;


        You can view the results of the above example in SQL Fiddle.



        EDIT



        For better results the above query could be re-written:



        SELECT c1,c2,c3,c4,c5,c6,
        CASE TRUE
        WHEN HOUR(c6) BETWEEN 0 AND 1 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 0
        WHEN HOUR(c6) BETWEEN 2 AND 3 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 2
        WHEN HOUR(c6) BETWEEN 4 AND 5 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 4
        WHEN HOUR(c6) BETWEEN 6 AND 7 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 6
        WHEN HOUR(c6) BETWEEN 8 AND 9 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 8
        WHEN HOUR(c6) BETWEEN 10 AND 11 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 10
        WHEN HOUR(c6) BETWEEN 12 AND 13 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 12
        WHEN HOUR(c6) BETWEEN 14 AND 15 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 14
        WHEN HOUR(c6) BETWEEN 16 AND 17 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 16
        WHEN HOUR(c6) BETWEEN 18 AND 19 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 18
        WHEN HOUR(c6) BETWEEN 20 AND 21 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 20
        WHEN HOUR(c6) BETWEEN 22 AND 23 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 22
        END AS c7 FROM t1
        GROUP BY c7;


        You can view the results of the edited example in this SQL Fiddle.






        share|improve this answer















        Assuming a database table having a table with the following schema:



        CREATE TABLE t1 (
        c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        c2 VARCHAR(100),
        c3 VARCHAR(100),
        c4 DATE,
        c5 TIME,
        c6 DATETIME);

        INSERT INTO t1 (c1,c2,c3,c4,c5,c6)
        VALUES
        (1,'DDA1123_12112018','55307','2018-12-11','00:43:00', '2018-12-11 00:43:00'),
        (2,'DDA1123_12112018','55307','2018-12-11','00:07:00', '2018-12-11 00:07:00'),
        (3,'DDA1124_12112018','55308','2018-12-11','03:07:00', '2018-12-11 03:07:00'),
        (4,'DDA1124_12112018','55308','2018-12-11','04:07:00', '2018-12-11 04:07:00'),
        (5,'DDA1124_12112018','55308','2018-12-11','07:07:00', '2018-12-11 07:07:00'),
        (6,'DDA1125_12112018','55309','2018-12-11','06:07:00', '2018-12-11 06:07:00'),
        (7,'DDA1125_12112018','55309','2018-12-11','07:57:00', '2018-12-11 07:57:00'),
        (8,'DDA1126_12112018','55310','2018-12-11','08:07:00', '2018-12-11 08:07:00'),
        (9,'DDA1127_12112018','55311','2018-12-11','12:07:00', '2018-12-11 12:07:00'),
        (10,'DDA1127_12112018','55311','2018-12-11','14:07:00', '2018-12-11 14:07:00'),
        (11,'DDA1127_12112018','55311','2018-12-11','17:07:00', '2018-12-11 17:07:00');


        In that case you could use CASE ... WHEN in GROUP BY like in this example query.



        SELECT c1,c2,c3,c4,c5,c6,
        CASE TRUE
        WHEN HOUR(c6) BETWEEN 0 AND 2 THEN 0
        WHEN HOUR(c6) BETWEEN 2 AND 4 THEN 2
        WHEN HOUR(c6) BETWEEN 4 AND 6 THEN 4
        WHEN HOUR(c6) BETWEEN 6 AND 8 THEN 6
        WHEN HOUR(c6) BETWEEN 8 AND 10 THEN 8
        WHEN HOUR(c6) BETWEEN 10 AND 12 THEN 10
        WHEN HOUR(c6) BETWEEN 12 AND 14 THEN 12
        WHEN HOUR(c6) BETWEEN 14 AND 16 THEN 14
        WHEN HOUR(c6) BETWEEN 16 AND 18 THEN 16
        WHEN HOUR(c6) BETWEEN 18 AND 20 THEN 18
        WHEN HOUR(c6) BETWEEN 20 AND 22 THEN 20
        WHEN HOUR(c6) BETWEEN 22 AND 24 THEN 22
        END AS c7 FROM t1
        GROUP BY c7;


        You can view the results of the above example in SQL Fiddle.



        EDIT



        For better results the above query could be re-written:



        SELECT c1,c2,c3,c4,c5,c6,
        CASE TRUE
        WHEN HOUR(c6) BETWEEN 0 AND 1 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 0
        WHEN HOUR(c6) BETWEEN 2 AND 3 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 2
        WHEN HOUR(c6) BETWEEN 4 AND 5 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 4
        WHEN HOUR(c6) BETWEEN 6 AND 7 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 6
        WHEN HOUR(c6) BETWEEN 8 AND 9 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 8
        WHEN HOUR(c6) BETWEEN 10 AND 11 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 10
        WHEN HOUR(c6) BETWEEN 12 AND 13 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 12
        WHEN HOUR(c6) BETWEEN 14 AND 15 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 14
        WHEN HOUR(c6) BETWEEN 16 AND 17 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 16
        WHEN HOUR(c6) BETWEEN 18 AND 19 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 18
        WHEN HOUR(c6) BETWEEN 20 AND 21 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 20
        WHEN HOUR(c6) BETWEEN 22 AND 23 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 22
        END AS c7 FROM t1
        GROUP BY c7;


        You can view the results of the edited example in this SQL Fiddle.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 14:26

























        answered Nov 26 '18 at 16:45









        Peter DarmisPeter Darmis

        5,74111226




        5,74111226






























            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%2f53348367%2fgroup-by-range-of-datetime-mysql%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python