Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PostgreSQL: Select only past three months of data

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I make this work?

NOTE: I’m using PostgreSQL on Metabase.

>Solution :

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';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading