2

我有这个查询:

select count(*) from logins group by user_id

这将返回每个用户的用户登录号:

数数(*)


10

23

35

54

使用单一且高效的查询,如何选择(计算)这个的中间值?中等 = (10+23+35+54)/4。

4

5 回答 5

3
select avg(num_login) 
  from (select count(*) as num_login 
  from logins group by user_id) as num_logins 
于 2012-07-23T13:47:11.990 回答
0
select avg(count(*)) from logins group by user_id
于 2012-07-23T13:45:09.923 回答
0
SELECT AVG(c) FROM (SELECT COUNT(*) AS c FROM logins GROUP BY user_id) AS v

这有效..

于 2012-07-23T13:49:55.233 回答
0

使用子查询,然后AVG对子查询的结果起作用:

SELECT AVG(logins_cnt) AS logins_cnt
FROM   (SELECT COUNT(1) AS logins_cnt
        FROM logins 
        GROUP BY user_id
       ) a;
于 2012-07-23T13:51:18.923 回答
-1

你试过平均功能吗?

select avg(select count(*) from logins group by user_id)
于 2012-07-23T13:46:39.690 回答