我有这个查询:
select count(*) from logins group by user_id
这将返回每个用户的用户登录号:
数数(*)
10
23
35
54
使用单一且高效的查询,如何选择(计算)这个的中间值?中等 = (10+23+35+54)/4。
select avg(num_login)
from (select count(*) as num_login
from logins group by user_id) as num_logins
select avg(count(*)) from logins group by user_id
SELECT AVG(c) FROM (SELECT COUNT(*) AS c FROM logins GROUP BY user_id) AS v
这有效..
使用子查询,然后AVG
对子查询的结果起作用:
SELECT AVG(logins_cnt) AS logins_cnt
FROM (SELECT COUNT(1) AS logins_cnt
FROM logins
GROUP BY user_id
) a;
你试过平均功能吗?
select avg(select count(*) from logins group by user_id)