Filter for last X days

Filter for last X days

Description

Often you only want to return the last X days of a query (e.g. last 30 days) and you want that to always be up to date (dynamic).

This snippet can be added to your WHERE clause to filter for the last X days:

SELECT
   <COL1>,...<COLN>,
   <DATE_COLUMN>
FROM
   <TABLE>
WHERE
   <DATE_COLUMN> > current_date - interval 'X' day

where:

  • <COL1>,...<COLN> are all the columns you want to return in your query
  • <DATE_COLUMN> is your date column you want to filter on
  • X is the number of days you want to filter on

For reference see Postgresā€™s date/time Functions and Operators.