我有一个带有非唯一名称字段的MySql表,我想查询以下内容;
对于每个名称值(“John”)计算有多少条记录(32条记录名为John),我最终会将其写入每条记录。
平均每个名称组的计数(例如,带有John的32个记录和带有Sally的42个记录,表37记录的平均值)
计数计数含义,例如6个记录组,10个20记录组等
我可以使用一个或多个查询执行此操作吗?
答案 0 :(得分:1)
表组的平均值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;
答案 1 :(得分:0)
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