1

所以我有一个查询:

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

);

4

3 回答 3

2

您可以将当前查询用作获取每个州县数平均值的子查询

Select avg (c) average_counties
From (select count(numberofcounty) c
from countytable 
group by states) s
于 2013-10-13T22:56:16.663 回答
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

于 2013-10-13T23:05:20.913 回答
0

如果我没有遗漏任何东西,这基本上应该给您与其他答案中的双重分组方法相同的结果:

SELECT COUNT(numberofcounty) / COUNT(DISTINCT states)
FROM countytable
;
于 2013-10-15T11:21:31.313 回答