Calculate variation of IP addresses column using MySQL

做~自己de王妃 提交于 2019-12-04 20:14:08

plan

  • get count grouping by ip
  • divide by ( cross-joining ) the total rowcount
  • take maximum repeat ratio from above

setup

create table example
(
  id integer primary key auto_increment not null,
  ip varchar(13) not null
);

insert into example
( ip )
values
( '89.46.74.56'   ),
( '89.46.74.56'   ),
( '89.46.74.56'   ),
( '91.14.37.249'  ),
( '104.233.103.6' )
;

query

select max(repeat_factor)
from
(
select ip, count(*) / rc.row_count as repeat_factor
from example
cross join ( select count(*) as row_count from example ) rc
group by ip
) q
;

output

+--------------------+
| max(repeat_factor) |
+--------------------+
| 0.6                |
+--------------------+

sqlfiddle

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