Skip to content

Instantly share code, notes, and snippets.

@zelark
Created April 14, 2014 07:36
Show Gist options
  • Save zelark/10624657 to your computer and use it in GitHub Desktop.
Save zelark/10624657 to your computer and use it in GitHub Desktop.
Calculate the avarage without non-zero values and given zero values in the next column.
with data as (
select 50 as col1, 0 as col2 from dual union all
select 0 as col1, 20 as col2 from dual union all
select 20 as col1, 10 as col2 from dual union all
select 20 as col1, 10 as col2 from dual
)
select avg(nullif(col1, 0)) as col1_avg_no_zero,
avg(nullif(case when col2 = 0 then 0 else col1 end, 0)) as col1_avg_no_zero_with_col2
from data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment