Search code examples
mysqlsqlgroup-byutc

MySQL Group By Date when I have UTC


I have data in a MySQL database that contains a value and the UTC time that value was entered, like so:

----value---|-----------utc---------
     8      |    2021-05-28 16:59:24
------------------------------------
    12      |    2021-05-28 20:51:11
------------------------------------
     3      |    2020-12-12 13:45:34
------------------------------------

With this, how could I group these values by their UTC date but not time, something like the pseudo-query SELECT value, utc GROUP BY utc would return the sum of all the values grouped by date like:

----value---|-----------date---------
     20     |      2021-05-28
------------------------------------
      3     |      2020-12-12
------------------------------------

Any help would be greatly appreciated, thank you!


Solution

  • Just use the date() function:

    select date(utc), sum(value)
    from t
    group by date(utc);
    

    Or for a particular date:

    select sum(value)
    from t
    where utc >= '2021-05-08' and
          utc < '2021-05-09'