我有一个带有非唯一名称字段的 MySql 表,我想查询以下内容;
对于每个名称值(“John”)计算有多少条记录(名称为 John 的 32 条记录),我最终会将其写入每条记录。
平均每个名称组的计数(例如,John 的 32 条记录和 Sally 的 42 条记录,表 37 记录的平均值)
计数意味着例如 6 个 10 记录组、10 个 20 记录组等
我可以通过一个或多个查询来做到这一点吗?
表组的平均值http://sqlfiddle.com/#!2/f5af3/5:
SELECT
AVG(ct)
FROM (
SELECT
COUNT(name) as ct
FROM
names
GROUP BY
name) as temp1;
按计数计数http://sqlfiddle.com/#!2/b0628/1:
SELECT
COUNT(ct) AS "Count_per_count",
ct as "Actual Count"
FROM (
SELECT
COUNT(name) as ct
FROM
names
GROUP BY
name) as temp1
GROUP BY ct;
select name, count(*) as num from table group by name;
select ((select count(*) from table) / (select count(distinct name) from table));
select count(*) as num, type from
(select count(*) as type from table group by name) t
group by type