Skip to content

Instantly share code, notes, and snippets.

Created October 17, 2017 13:40
Show Gist options
  • Save anonymous/ef1f5bb79eff2100295430132ceae951 to your computer and use it in GitHub Desktop.
Save anonymous/ef1f5bb79eff2100295430132ceae951 to your computer and use it in GitHub Desktop.
WITH d AS
(
SELECT *
FROM atable
WHERE date >= CURRENT_TIMESTAMP - INTERVAL '1 YEAR'
)
SELECT *
FROM d
UNION ALL
SELECT *
FROM atable
WHERE date < CURRENT_TIMESTAMP - INTERVAL '1 YEAR'
ORDER BY date DESC
LIMIT GREATEST(0, 50 - (SELECT COUNT(*) FROM d)) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment