I have a table with one numeric value (n) and three string values (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?
我有一个表有一个数值(n)和三个字符串值(a,b,c)。如何查询此表,以便只获得(a,b,c)的不同值,如果有重复项,则取相应的n个值的最大值?
2 个解决方案
#1
select max(n), a, b, c
from mytable
group by a, b, c
#2
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的唯一或不同集合,并显示该集合中找到的最大n。
MAX
is an aggregate function designed for use with GROUP BY
. Other potentially useful aggregate functions include MIN
, AVERAGE
, and COUNT
.
MAX是一个集合函数,设计用于GROUP BY。其他可能有用的聚合函数包括MIN,AVERAGE和COUNT。
#1
select max(n), a, b, c
from mytable
group by a, b, c
#2
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的唯一或不同集合,并显示该集合中找到的最大n。
MAX
is an aggregate function designed for use with GROUP BY
. Other potentially useful aggregate functions include MIN
, AVERAGE
, and COUNT
.
MAX是一个集合函数,设计用于GROUP BY。其他可能有用的聚合函数包括MIN,AVERAGE和COUNT。