Updated SQL to be Postgres (rather than Redshift) compatible