Fixed a query to work with PG