SQL汇总功能带有子征服
我有这两个表:
public.country
列 | 类型 | 排序 | 规则 | 可为空默认 |
---|---|---|---|---|
名称 | 文本 | 不为空 | ||
two_letter | 文本 | 不为空 | ||
country_id | 整数 | 不为空 |
索引:
“country_pkey”主键,btree(两个字母)
public.subcountry
列 | 类型 | 排序 | 规则 | 可为空默认 |
---|---|---|---|---|
国家/地区 | 文本 | 不为空 | ||
subcountry_name | 文本 | 不为空细分 | ||
文本 | subcountry_level | |||
文本 |
- 索引: <前><代码>“subcountry_country_subcountry_name_key” 唯一约束,btree(国家/地区,子国家/地区名称)
外键约束:
<前><代码>“subcountry_country_fkey” 外键(国家/地区) 参考国家/地区(两个字母)
我想找到“two_letter”以字母“A”和“Z”开头的国家/地区的平均子国家/地区数量。我希望输出是这样的:
PREFIX | Subcountry Average
-------+-------------------
A | some_number
Z | some_number
根据我的理解,上面的输出可以通过使用GROUP BY
来实现。我知道正确的方法是计算以指定字母开头的 two_letter
所拥有的所有 subcountry
,并将其除以该 two_letter
的数量>s。这是我对以字母 A 开头的 two_letter
的尝试:
SELECT COUNT(countryA) AS countryAsum
FROM (
SELECT country.name AS countryA
FROM country
WHERE country.two_letter LIKE 'A_'
) a;
-- result: 16
SELECT COUNT(subcountryA) as countryAsubcountrySum
FROM (
SELECT subcountry.country AS subcountryA
FROM subcountry, country
WHERE country.two_letter LIKE 'A_' AND country.two_letter = subcountry.country
) a;
-- result: 232
-- hence, the wanted output is: 232 / 16 = 14.5
现在,我只需要将 countryAsubcountrySum
除以 countryAsum
并将其设为 1仅查询。问题是,我不知道怎么办。不仅如此,我还必须将其设为对以字母 Z 开头的 two_letter
的单个查询。任何帮助将不胜感激。 (请原谅我糟糕的英语)
I have these two tables:
public.country
Column | Type | Collation | Nullable | Default |
---|---|---|---|---|
name | text | not null | ||
two_letter | text | not null | ||
country_id | integer | not null |
Indexes:
"country_pkey" PRIMARY KEY, btree (two_letter)
public.subcountry
Column | Type | Collation | Nullable | Default |
---|---|---|---|---|
country | text | not null | ||
subcountry_name | text | not null | ||
subdivision | text | |||
subcountry_level | text |
Indexes:
"subcountry_country_subcountry_name_key" UNIQUE CONSTRAINT, btree (country, subcountry_name)
Foreign-key constraints:
"subcountry_country_fkey" FOREIGN KEY (country) REFERENCES country(two_letter)
I want to find the average number of subcountries that countries whose `two_letter' starts with the letters "A" and "Z" have. I want the output to be like this:
PREFIX | Subcountry Average
-------+-------------------
A | some_number
Z | some_number
The output above is, according to my understanding, achievable by using GROUP BY
. I know the way to go is to count all subcountry
that the two_letter
that starts with the specified letter has and divide it by the amount of that two_letter
s. Here is my attempt for the two_letter
s that start with the letter A:
SELECT COUNT(countryA) AS countryAsum
FROM (
SELECT country.name AS countryA
FROM country
WHERE country.two_letter LIKE 'A_'
) a;
-- result: 16
SELECT COUNT(subcountryA) as countryAsubcountrySum
FROM (
SELECT subcountry.country AS subcountryA
FROM subcountry, country
WHERE country.two_letter LIKE 'A_' AND country.two_letter = subcountry.country
) a;
-- result: 232
-- hence, the wanted output is: 232 / 16 = 14.5
Now, I only need to divide countryAsubcountrySum
by countryAsum
and make it one query only. The problem is, I don't know how. And not only that, I also have to make this a single query for two_letter
s that start with the letter Z. Any help would be appreciated. (and pardon my bad English)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论