MYSQL calculating an average on a count

2019-03-05 16:53发布

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

2条回答
老娘就宠你
2楼-- · 2019-03-05 17:11

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

SELECT count(*) / count(distinct Opname_OpnameID)
FROM behandeling
查看更多
甜甜的少女心
3楼-- · 2019-03-05 17:23

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;
查看更多
登录 后发表回答