简体   繁体   中英

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). 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?

select max(n), a, b, c
from mytable
group by a, b, c

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM