简体   繁体   English

Noob关注:子查询计算

[英]Noob Concern: Sub-query calculations

I've got a table bbc with the following columns: 我有一个表bbc,其中包含以下列:

name (refers to a name of a country within a particular region of the world) 名称 (指世界特定区域内的国家名称)

region (continent of the world) 区域 (世界大陆)

population (population of the country in the name name field) The question I'm trying to answer: 人口 (国家名称字段中的国家人口)我想回答的问题:

The question is as follows: 问题如下:
"Some countries have populations more than three times that of any of their neighbours (in the same region). Give the countries and regions." “有些国家的人口是其邻国(在同一地区)的三倍以上。请提供这些国家和地区。”

I was thinking the answer might be something like: 我在想答案可能是这样的:

SELECT a.name, a.region FROM bbc AS a
WHERE a.region IN
     (
        SELECT b.region FROM bbc AS b 
        GROUP By b.region 
        HAVING MIN(b.population) < 3*b.population)

But honestly, I lose it at that last line... I have no idea how I would find counteries that have more than three times that of any of their neighbours in the same region! 但老实说,我在最后一行中输了它……我不知道如何找到比同地区任何邻国的三倍还多的计数器! Quite tough. 相当艰难。 O_o O_O

Any and all help would be appreciated. 任何和所有帮助将不胜感激。

select
   a.name, a.region
from bbc as a
where 
    a.population >
    (
        select 3*max(b.population)
        from bbc as b
        where b.region = a.region and b.name <> a.name
    )
Select
  a.name,
  a.region
From
  bbc as a
Where
  Not Exists (
    Select
      'x'
    From
      bbc as b
    Where
      a.region = b.region And
      a.name != b.name And
      a.population < 3 * b.population
  )
SELECT name, continent from world x
WHERE (SELECT population from world y
          where y.name = x.name 
           and y.continent = x.continent
           and population > 0) > ALL 
(SELECT 3*population from world z 
          where z.continent = x.continent 
             and z.name != x.name
          and population > 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM