我有一个计算行平均值的查询。单行的平均值由非空/空值组成。这很有效。 我现在想要的是只计算行的平均值大于某个数字。现在它正在使用所有行。 我怎么能这样做?
id | p_cijfer | b_cijfer | c_cijfer | c_id
----------------------------------------------
1 | 4 | 4 | 0 | 3 average = 4
----------------------------------------------
2 | 8 | 6 | 6 | 3 average = 6.7
----------------------------------------------
3 | 10 | 6 | 8 | 3 average = 8
----------------------------------------------
1 | 4 | 4 | 0 | 1 average = 4
----------------------------------------------
现在我只想计算平均值高于6的行的平均数。这将导致仅计算记录2和3。 输出必须是id的2&的平均值。 3,平均值=(6.7 + 8)/2=7.5
select
AVG(((reviews.prijs_cijfer+
reviews.planning_cijfer+
reviews.betrouwbaarheid_cijfer +
reviews.communicatie_cijfer +
reviews.kennis_cijfer +
reviews.innovatie_cijfer
) /
nullif(
case when prijs_cijfer=0 then 0 else 1 end +
case when planning_cijfer=0 then 0 else 1 end +
case when betrouwbaarheid_cijfer=0 then 0 else 1 end +
case when communicatie_cijfer=0 then 0 else 1 end +
case when kennis_cijfer=0 then 0 else 1 end +
case when innovatie_cijfer=0 then 0 else 1 end , 0)))
from reviews
答案 0 :(得分:2)
您可以使用子查询来应用过滤器。
select c_id, avg(rows.rowavg) filteredavg, count(rows.rowavg) affecting
from (
select
c_id c_id,
((reviews.prijs_cijfer+
reviews.planning_cijfer+
reviews.betrouwbaarheid_cijfer +
reviews.communicatie_cijfer +
reviews.kennis_cijfer +
reviews.innovatie_cijfer
) /
nullif(
case when prijs_cijfer=0 then 0 else 1 end +
case when planning_cijfer=0 then 0 else 1 end +
case when betrouwbaarheid_cijfer=0 then 0 else 1 end +
case when communicatie_cijfer=0 then 0 else 1 end +
case when kennis_cijfer=0 then 0 else 1 end +
case when innovatie_cijfer=0 then 0 else 1 end , 0)) rowavg
from reviews
) rows
where rows.rowavg > 6
group by c_id