HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

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

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
groupcolumnreturnnullvaluedefaultselected

Problem

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.

Solution

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).

Context

StackExchange Database Administrators Q#69059, answer score: 6

Revisions (0)

No revisions yet.