[英]Selecting the distinct values from three columns with the max of a fourth where there are duplicates
I have a table with one numeric value (n) and three string values (a,b,c). 我有一个带有一个数值(n)和三个字符串值(a,b,c)的表。 How do I query this table so that I get only distinct values of (a,b,c) and if there are duplicates, take the maximum of the corresponding set of n values?
如何查询此表,以便仅获得(a,b,c)的不同值,如果存在重复项,则取相应的一组n值的最大值?
select max(n), a, b, c
from mytable
group by a, b, c
Use GROUP BY
: 使用
GROUP BY
:
select a, b, c, max(n)
from table
group by a, b, c;
This will show only unique or distinct sets of a, b, c
and show the maximum n
found in that set. 这将仅显示
a, b, c
唯一或不同集合a, b, c
并显示在该集合中找到的最大n
。
MAX
is an aggregate function designed for use with GROUP BY
. MAX
是设计用于GROUP BY
的聚合函数。 Other potentially useful aggregate functions include MIN
, AVERAGE
, and COUNT
. 其他可能有用的聚合函数包括
MIN
, AVERAGE
和COUNT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.