所以我有查询:
select states, count(numberofcounty)
from countytable
group by states
order by count(distinct numberofcounty) ASC;
返回2列的表:州的数量和县的数量从最少到最多。 如何在一个实数中获得每个州有多少个县的平均值?
表结构是:
create table numberofcounty(
numberofcounty text,
states text references states(states),
);
create table states (
states text primary key,
name text,
admitted_to_union text
);
答案 0 :(得分:2)
您可以将当前查询用作子查询,以获得每个州的县数平均值
Select avg (c) average_counties
From (select count(numberofcounty) c
from countytable
group by states) s
答案 1 :(得分:2)
这可能是吗?
countytable:
state|county
a aa
a ab
a ac
b ba
SELECT AVG(counties) FROM (SELECT state, COUNT(county) as counties FROM countytable GROUP BY state) as temp
结果:2
答案 2 :(得分:0)
如果我没有遗漏任何内容,这应该基本上给你与其他答案中的双重分组方法相同的结果:
SELECT COUNT(numberofcounty) / COUNT(DISTINCT states)
FROM countytable
;