Reputation: 91
I have a table with two columns, caloriesConsumed
and createdAt
with datatypes Int
and DateTime
respectively. I want to get the sum of the numbers entered on the same date.
SELECT SUM(caloriesConsumed), createdAt
FROM record
GROUP BY createdAt;
As createdAt
is of datatypeDateTime
, the columns are not grouped and returns all the records. How can I group by the date and ignore the time in the column?
Upvotes: 1
Views: 433
Reputation: 37493
use a cast(createdAt as date)
to convert the datetime to date
select sum(caloriesConsumed), cast(createdAt as date)
from record
group by cast(createdAt as date)
Upvotes: 2