SQL汇总功能带有子征服

发布于 2025-01-20 11:19:29 字数 2300 浏览 3 评论 0原文

我有这两个表:

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

ColumnTypeCollationNullableDefault
nametextnot null
two_lettertextnot null
country_idintegernot null
  • Indexes:

     "country_pkey" PRIMARY KEY, btree (two_letter)
    

public.subcountry

ColumnTypeCollationNullableDefault
countrytextnot null
subcountry_nametextnot null
subdivisiontext
subcountry_leveltext
  • 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_letters. Here is my attempt for the two_letters 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_letters that start with the letter Z. Any help would be appreciated. (and pardon my bad English)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文