Search code examples
postgresqldatecountsum

How to get sum of int with date in sql query?


I have the following query and I would like to add all the views of a specific date as shown below.

SELECT SUM("videoView"."views"), "videoView"."startDate"::timestamp::date
FROM "videoView" 
WHERE "videoView"."videoId" =23 
AND ("videoView"."startDate"::timestamp::date)= '2021/11/25'
GROUP BY "videoView"."startDate"

The result I want is:

sum     date
3       2021/11/25

The result i am getting is

sum      date 
2        2021/11/25
1        2021/11/25

Solution

  • As mentioned by @joachim-isaksson, You can do this like following:

    SELECT SUM("videoView"."views"), "videoView"."startDate"::timestamp::date
    FROM "videoView" 
    WHERE "videoView"."videoId" =23 
    AND ("videoView"."startDate"::timestamp::date)= '2021/11/25'
    GROUP BY "videoView"."startDate"::timestamp::date;