Techy
Techy

Reputation: 91

Query to get sum of a column based on the date of it's created datetime?

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

Answers (1)

Fahmi
Fahmi

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

Related Questions