【问题标题】:How to get the group that has the MAX value for the grouped column如何获取分组列具有最大值的组
【发布时间】:2019-06-28 22:38:53
【问题描述】:

我有一个表 KIDS 有一个列 AGE。 我想使用 SQL 查询来获取最大孩子的所有记录。

例如:如果我有

Name   Age
----------
David   10 
Dan     10
Leah     8 
Hannah   6

我想要获取 David 和 Dan 的记录。

【问题讨论】:

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


【解决方案1】:

你可以试试下面 -

select * from tablename
where age in (select max(age) from tablename)

【讨论】:

    【解决方案2】:

    使用max()

      select * from t where age = (select max(age) from t)
    

    【讨论】:

      【解决方案3】:

      您可以应用以下代码:

      select * from old_Records where age =(select max(age) from old_Records)
      

      【讨论】:

        猜你喜欢
        • 2018-06-10
        • 1970-01-01
        • 2018-11-21
        • 2015-03-15
        • 2023-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多