SQL group by: using where-clause logic to filter results based on aggregate functions

喜夏-厌秋 提交于 2019-12-21 20:17:30

问题


I have a basic group by/avg statement:

select url, avg(contentping+tcpping), count(*) from websites ws, ping pi 
where ws.idwebsite = pi.idwebsite and errortype is null
group by url order by avg(contentping+tcpping) asc;

What I want to do now is to drop any results which have a higher than average ping of 500. How can I do this...?


回答1:


just add a having clause:

select url, avg(contentping+tcpping), count(*) from websites ws, ping pi 
where ws.idwebsite = pi.idwebsite and errortype is null
group by url 
having avg(contenetping+tcpping) < 500
order by avg(contentping+tcpping) asc;


来源:https://stackoverflow.com/questions/2480613/sql-group-by-using-where-clause-logic-to-filter-results-based-on-aggregate-func

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!