From ef992b64491465455799cfe74210312273f7f55e Mon Sep 17 00:00:00 2001 From: prihoda Date: Sat, 9 Apr 2016 05:54:04 +0200 Subject: [PATCH] Fix week and month Time grain in MySQL (#297) With mysql datetime and timestamp columns, currently the Time grain "week" and "month" options don't remove the time part. This results in groupings like this: timestamp count 2015-04-05 07:00:00 1 2015-04-05 10:00:00 1 2015-04-05 11:00:00 2 2015-04-05 11:50:00 1 2015-04-05 12:00:00 5 2015-04-05 14:20:00 1 2015-04-05 14:30:00 1 and so on. This is solved by wrapping the DATE_SUB with DATE(). --- caravel/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/caravel/models.py b/caravel/models.py index 96df0c6aaa..ed95467e50 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -337,8 +337,10 @@ class Database(Model, AuditMixinNullable): 'mysql': ( Grain('Time Column', '{col}'), Grain('day', 'DATE({col})'), - Grain('week', 'DATE_SUB({col}, INTERVAL DAYOFWEEK({col}) - 1 DAY)'), - Grain('month', 'DATE_SUB({col}, INTERVAL DAYOFMONTH({col}) - 1 DAY)'), + Grain("week", "DATE(DATE_SUB({col}, " + "INTERVAL DAYOFWEEK({col}) - 1 DAY))"), + Grain("month", "DATE(DATE_SUB({col}, " + "INTERVAL DAYOFMONTH({col}) - 1 DAY))"), ), 'postgresql': ( Grain("Time Column", "{col}"),