ERROR: syntax error at or near "ORDER" LINE 5: ORDER BY DT.id, F.refiner IS NOT NULL, CASE WHEN F.ref... ^