MYSQL calculating an average on a count

故事扮演 提交于 2019-12-20 06:23:32

问题


I have a simple query that I want an average on. This is what it looks like now, and I want to know the average of my count per Opname_OpnameID.

SELECT Opname_OpnameID, count(*) as 'behandelingen per opname'
FROM behandeling
GROUP BY Opname_OpnameID

回答1:


If you want the average count, presumably over the entire table, then just do exactly that:

SELECT AVG(cnt) AS total_avg
FROM ( 
    SELECT COUNT(*) AS cnt FROM behandeling GROUP BY Opname_OpnameID
) t;



回答2:


You can use count(distinct) and not use a subquery:

SELECT count(*) / count(distinct Opname_OpnameID)
FROM behandeling


来源:https://stackoverflow.com/questions/50332526/mysql-calculating-an-average-on-a-count

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