Home > Back-end >  PostgreSQL: Select only past three months of data
PostgreSQL: Select only past three months of data

Time:12-17

I am trying to select only the past three months of data from a table. I've attempted the following:

SELECT *
FROM cte_data
WHERE  DATEDIFF(MONTH, my_timestamp, GETDATE()) <= 3

This is returning the error: ERROR: column "month" does not exist Position: 1182

My date follows the format: December 16, 2021, 2:40 PM

How can I make this work?

NOTE: I'm using PostgreSQL on Metabase.

CodePudding user response:

As documented in the manual, there is no datediff() function in Postgres. You just subtract an interval:

select *
from cte_data
where my_timestamp >= current_timestamp - interval '3 month';
  • Related