Sample Header Ad - 728x90

How to retrieve data based on year to date in Postgres?

5 votes
2 answers
582 views
I'd like to retrieve data from "year to date" in my below query. I was hoping for something simple in the 'INTERVAL' but could not find anything. I have the following:
SELECT
    "status_id", date_part('epoch', time) * 1000 as time,
    COUNT(time) as "count"
FROM evStatus
WHERE time >= NOW() - Interval '{1}'
GROUP BY "status_id", time
I'd like to grab data from January of what ever year it is to current date. I tested this below and it appears to work but want to make sure this would be the correct way?
SELECT
    "status_id", date_part('epoch', time) * 1000 as time,
    COUNT(time) as "count"
WHERE time BETWEEN date_trunc('year', now()) AND CURRENT_TIMESTAMP
Asked by muttBunch (65 rep)
Mar 1, 2025, 09:45 PM
Last activity: Mar 3, 2025, 03:01 PM