I'm trying to output and label a column total from a rollup.
select coalesce(column_1, 'Total') as coalesced_value,
sum(column_2) as column_sum
from table
where yada yada
group by rollup(coalesced_value)
order by coalesced_value
The query works fine and generates a total as expected, but the column value that I would expect to be 'Total' shows up as [null].
There's a good chance it's probably some lack of understanding on my part, but then there's this that says that PostgreSQL's COALESCE is non-standard and makes me wonder if it's really me.
To quote:
COALESCE on row types
The spec defines COALESCE(X,Y) as a syntactic transformation to CASE WHEN X IS NOT NULL THEN X ELSE Y END (it leaves open the question of whether X is really evaluated twice by disallowing non-deterministic expressions or expressions with side effects in this context). A consequence of this is that the rather odd rules for null tests of row types are applied. PostgreSQL applies only the "is not the null value" test to X. Accordingly, if X is a row value containing null columns, PostgreSQL will return X, while the spec would require returning Y.
(Lovely explanation, right?)
I also ran across some information that indicates that COALESCE data types have to match, otherwise the function fails silently. (!)
I would expect the string literal 'Total' to be interpreted as a varchar, and column_1 is defined in the database as a varchar(12), but at this point I am not sure of much of anything, and any help would be most appreciated.
coalesce(some_row_value, row(1,2,3))) so it doesn't have anything to do with simplevarchars, the text makes more sense when you realize thatXandYin that context are compound values. Looks like you're getting the string'[null]'somewhere in yourcolumn_1values so everything is working fine and you're being tricked by an odd value in the column (which probably came from a broken application that was writing data to your table). So look at your table and see if you can produce a minimal example.