简体   繁体   English

MYSQL计算计数平均值

[英]MYSQL calculating an average on a count

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. 这就是现在的样子,我想知道每个Opname_OpnameID的平均计数。

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

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;

You can use count(distinct) and not use a subquery: 您可以使用count(distinct)而不使用子查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM