Modified GROUP clause to handle both postgres 8.4 and 9.1 versions