如何在SQL表中获取最大行?请参见下面的示例表格。

huangapple go评论55阅读模式
英文:

how to get the max row in a sql table? see sample table below

问题

如何在 SQL 查询中仅返回每个 ID 的最大值(如果该 ID 有多于两行)?

SELECT ID, MAX(Value) AS Value
FROM your_table
GROUP BY ID;
英文:

I got sql table below:

ID   Value
10    5
10    6
11    6	
12    7
13   10
13   11

*How to return it in sql query where only the max value per ID (if the ID got more than two rows)?

ID |  Value
10    6
11    6	
12    7
13   11

答案1

得分: 3

按您想要唯一的列进行分组。诸如 max() 之类的聚合函数适用于每个组。

选择 idmax(value)
 your_table
 id 分组
英文:

Group by the column you want to be unique. Aggregation functions like max() apply to each group

select id, max(value)
from your_table
group by id

huangapple
  • 本文由 发表于 2023年7月3日 15:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定