假设有一个表demoa
,其中包含以下数据:
date a b c
2017-05-22 1 6 10
2017-05-23 2 7 11
2017-05-24 3 8 12
2017-05-25 4 9 null
2017-05-26 5 null null
2017-05-27 null null null
我想将null
值替换为其相应列的平均值:
date a b c
2017-05-22 1 6 10
2017-05-23 2 7 11
2017-05-24 3 8 12
2017-05-25 4 9 11
2017-05-26 5 7.5 11
2017-05-27 3 7.5 11
我使用nvl
尝试avg()
,但这需要每列group by
,并且无法移除null
值:
select
date,
nvl(a,avg(a)),
nvl(b,avg(b)),
nvl(c,avg(c))
from damoa
group by date,a,b,c;
感谢任何帮助。
答案 0 :(得分:2)
Windows功能 -
avg(...) over ()
select dt
,coalesce (a,avg(a) over ()) as a
,coalesce (b,avg(b) over ()) as b
,coalesce (c,avg(c) over ()) as c
from demoa
+------------+-----+-----+------+
| dt | a | b | c |
+------------+-----+-----+------+
| 2017-05-22 | 1.0 | 6.0 | 10.0 |
| 2017-05-23 | 2.0 | 7.0 | 11.0 |
| 2017-05-24 | 3.0 | 8.0 | 12.0 |
| 2017-05-25 | 4.0 | 9.0 | 11.0 |
| 2017-05-26 | 5.0 | 7.5 | 11.0 |
| 2017-05-27 | 3.0 | 7.5 | 11.0 |
+------------+-----+-----+------+