HCSVLAB-944: Change SQL query to be PostgreSQL 8.4 friendly