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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
select max(n), a, b, c
from mytable
group by a, b, c
回答2:
Use 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.
MAX
is an aggregate function designed for use with GROUP BY
. Other potentially useful aggregate functions include MIN
, AVERAGE
, and COUNT
.