【问题标题】:SQL - select row of group with lowest column value grouped by otherSQL - 选择由其他分组的具有最低列值的组的行
【发布时间】:2022-01-01 21:58:10
【问题描述】:

你好,

我有这张桌子

a b c
One hello 1
two hello 2
three hi 3
four hi 4

我想选择按B值分组的最小“C”值行

我的输出应该是

a b c
One hello 1
three hi 3

我如何选择它们?

【问题讨论】:

  • 您的 DBMS 是什么?有些产品有一个很好的捷径来完成任务。
  • postgres。 c 列值是大数字..不是行号

标签: sql select


【解决方案1】:

这通常是row_number 的解决方案

select a, b, c
from (
    select *, 
        Row_Number() over(partition by b order by c) rn
    from t
)t
where rn=1

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2020-07-11
    • 2018-09-03
    • 2015-11-10
    • 2017-08-29
    • 1970-01-01
    • 2019-10-03
    • 2019-08-05
    • 2012-04-28
    相关资源
    最近更新 更多