I don't have an 8.4 handy but in more recent versions, the array_to_string
is ignoring your NULLs so the problem isn't array_agg
, it is array_to_string
.
For example:
=> select distinct state from orders;
state
---------
success
failure
That blank line is in fact a NULL. Then we can see what array_agg
and array_to_string
do with this stuff:
=> select array_agg(distinct state) from orders;
array_agg
------------------------
{failure,success,NULL}
=> select array_to_string(array_agg(distinct state), ',') from orders;
array_to_string
-----------------
failure,success
And the NULL disappears in the array_to_string
call. The documentation doesn't specify any particular handling of NULLs but ignoring them seems as reasonable as anything else.
In version 9.x you can get around this using, as usual, COALESCE:
=> select array_to_string(array_agg(distinct coalesce(state, '')), ',') from orders;
array_to_string
------------------
,failure,success
So perhaps this will work for you:
array_to_string(array_agg(DISTINCT coalesce(class_low, '')), ',')
Of course that will fold NULLs and empty strings into one value, that may or may not be an issue.