Skip to content
Advertisement

Prevent NULLs on select average values

I’m trying to figure out how to prevent NULLs on return from a select and do not return anything (0 rows) for that query where I provide incorrect state value as

I have tried something like

but that didn’t work either.

Advertisement

Answer

Aggregate function withoutgroup by always returns a row. So you need to add some group by (for example, group by city) or having avg(lat) is not null to prevent output on an empty input dataset (filtered).

state |   alat
:---- | -----:
NY    | 2.0000
LA    |   null
| avg(lat) |
| -------: |
| avg(lat) |
| -------: |
|     null |
| avg(lat) |
| -------: |

db<>fiddle here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement