Postgres Subquery: CTEs, window functions and more
crunchydata.com