Return a default value in group by if selected column is null

Posted on

Question :

I am trying to run a group by statement which works fine however, one of the “group” is null. How do I return “Unknown” for that row?

For example:

    10000
A   9999
B   9990
C   4

etc.

First line I’d like to return as “Unknown” in the query.

Answer :

You should be able to use COALESCE(your_column, 'Unknown'):

The COALESCE function returns the first of its arguments that is not null.

This is not specific to GROUP BY, although you’ll certainly need to use it both in your SELECT column expression and in the GROUP BY expression (as usual when using GROUP BY).

Leave a Reply

Your email address will not be published. Required fields are marked *