I have a table of surveys in PostgreSQL. I want to find all the surveys that have been done since the start of the current season. I have written it this way:
WITH foo
AS (
SELECT
-- This case will find the date of the first day of this season
-- Year is selected based on the current date. This way we can use the same code next year.
-- ** finding the start of the season.
CASE
WHEN EXTRACT('month' FROM NOW()) >= 1 AND EXTRACT('month' FROM NOW()) < 4
THEN TO_TIMESTAMP( EXTRACT('year' FROM NOW())||'-01-01 01:00:00', 'YYYY-MM-DD HH:MI:SS')
WHEN EXTRACT('month' FROM NOW()) >= 4 AND EXTRACT('month' FROM NOW()) < 7
THEN TO_TIMESTAMP( EXTRACT('year' FROM NOW())||'-04-01 01:00:00', 'YYYY-MM-DD HH:MI:SS')
WHEN EXTRACT('month' FROM NOW()) >= 7 AND EXTRACT('month' FROM NOW()) < 9
THEN TO_TIMESTAMP( EXTRACT('year' FROM NOW())||'-07-01 01:00:00', 'YYYY-MM-DD HH:MI:SS')
WHEN EXTRACT('month' FROM NOW()) >= 10 AND EXTRACT('month' FROM NOW()) < 13
THEN TO_TIMESTAMP( EXTRACT('year' FROM NOW())||'-10-01 01:00:00', 'YYYY-MM-DD HH:MI:SS')
END start_season
)
SELECT *
FROM foo f,
my_table t
WHERE date_survey > start_season
This solution works. However, I am searching for a simpler solution if exists.