【问题标题】:SQLZOO- using GROUPBY to find the largest country in a continent; is this possible?SQLZOO- 使用 GROUPBY 查找一个大陆上最大的国家;这可能吗?
【发布时间】:2018-11-04 03:27:31
【问题描述】:

我正在处理 SQLZOO 的一个练习题,但我不确定为什么我尝试的解决方案不起作用,因为它对我有意义。

这是表格的格式::

 -------------------------------------------------------------
|     name      continent    area    population       gdp     |
|-------------------------------------------------------------|
| Afghanistan     Asia      652230    25500100    20343000000 |
| .                                                           |
| .                                                           |
| .                                                           |
|                                                             |
 -------------------------------------------------------------

问题如下:

找到每个大陆最大的国家(按区域),显示大陆、名称和区域。

这是我想解决的方法:

SELECT continent, name, area 
  FROM world
 WHERE name IN (SELECT continent, name, MAX(area) 
                  FROM world 
                 GROUP BY continent);

我知道这行不通,但为什么不呢?似乎嵌套的 SELECT 语句正在查找每个大陆的面积为 MAX 的国家,不是吗?

对此的实际解决方案如下:

SELECT continent, name, area 
  FROM world x
 WHERE area >= ALL
    (SELECT area 
       FROM world y
      WHERE y.continent=x.continent
        AND area>0)

但这似乎是一种复杂的方法;;这种方式最有意义吗?任何想法表示赞赏

提前谢谢你!!

【问题讨论】:

    标签: mysql sql greatest-n-per-group


    【解决方案1】:

    请试试这个查询,

    SELECT a.continent, name, a.area
    FROM
        (
            SELECT continent, max(area) as area
            FROM world 
            GROUP BY continent
            ORDER BY area desc
        ) a 
        join world b on a.area = b.area
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:
    select continent, name, area 
    from world x 
    where area = All (select MAX(area) from world where x.continent = continent)
    

    【讨论】:

      【解决方案3】:
      select continent, name, area
      from
      (select continent, name, area, rank() over(partition by continent order by area desc) as r1
      from world) a
      where a.r1 = 1
      

      【讨论】:

        【解决方案4】:

        乍一看,这个查询似乎有效

        SELECT continent, name, area 
          FROM world
         WHERE area IN (SELECT MAX(area) 
                          FROM world 
                         GROUP BY continent);
        

        Demo 1

        考虑到当前数据,在添加其他一些新记录时会出现一些问题,例如下面的演示。而不是上述更喜欢这个:

        SELECT w1.continent, name, w1.area 
          FROM world AS w1
          JOIN (SELECT continent, MAX(area) AS area
                  FROM world 
                 GROUP BY continent) AS w2
            ON w1.continent = w2.continent
           AND w1.area = w2.area
        

        Demo 2

        【讨论】:

        • 非常感谢!!你怎么这么快就想到了这个?
        • 如果您不介意我问,您是如何对 SQL 如此熟悉的?我正在练习,但仍然不舒服。谢谢
        • 不幸的是,这个答案是偶然的数据。如果某个大陆最大国家/地区的面积恰好与其他大陆第二大国家for example 的面积相同,它将返回额外的错误行。
        【解决方案5】:
        select A.continent, W.name, A.area
        from
        (select continent, max(area) as area from world group by continent)A, world W
        where
        A.continent = W.continent
        and
        A.area = W.area
        

        【讨论】:

          猜你喜欢
          • 2019-06-24
          • 2011-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-18
          • 2019-09-12
          • 1970-01-01
          相关资源
          最近更新 更多