【问题标题】:How to find an attribute's largest value based on another attribute?如何根据另一个属性找到一个属性的最大值?
【发布时间】:2014-11-01 01:55:10
【问题描述】:
Owner | Price
-------------
James | 8000
David | 1500
James | 20000
Kim   | 4500
Kim   | 9500

例如,如果我在上面的表格中显示了一些手表的所有者姓名及其价格。我怎样才能找出每个人拥有的最昂贵的手表?所以在这种情况下,结果表应该有 3 个元组。

【问题讨论】:

标签: sql database postgresql greatest-n-per-group psql


【解决方案1】:

GROUP BY 和 MAX 可以为您提供您想要的。

select Owner, MAX(price) as maxPrice
FROM Table1
Group by Owner

【讨论】:

  • 如果我不想在结果表中显示价格怎么办?
  • @user3300198,你能写出你的预期输出吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多