patternsqlMinor
Return a default value in group by if selected column is null
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:
etc.
First line I'd like to return as "Unknown" in the query.
For example:
10000
A 9999
B 9990
C 4etc.
First line I'd like to return as "Unknown" in the query.
Solution
You should be able to use
The COALESCE function returns the first of its arguments that is not null.
This is not specific to
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.