如何统计mySQL中高于平均水平的行?

时间:2017-07-27 20:18:06

标签: php mysql

这是我的查询,但是我无法成功显示pages的{​​{1}}高于列的平均值。

我在这里缺少什么?

percent_diff

2 个答案:

答案 0 :(得分:2)

我认为这将是解决方案:

SELECT count(page) FROM pages_diff
WHERE company_id = 3 AND percent_diff > (
  SELECT avg(percent_diff) FROM pages_diff
  WHERE company_id = 3
)

答案 1 :(得分:0)

有几种方法可以做到这一点。使用您的基本查询最快(写入)将是这样的:

SELECT above_avg,below_avg from
(
select count(page) as above_avg FROM pages_diff where company_id = 3 
group by page
having percent_diff > avg(percent_diff)
) nested_above
,
(
select count(page) as below_avg FROM pages_diff where company_id = 3 
group by page
having percent_diff < avg(percent_diff)
) nested_below

替代方案你可以获得avg然后使用if语句。你仍然会经历两次表(一次得到平均值,一次一次计算两列)