Selecting the distinct values from three columns with the max of a fourth where there are duplicates

自古美人都是妖i 提交于 2019-12-11 04:29:22

问题


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.



来源:https://stackoverflow.com/questions/467329/selecting-the-distinct-values-from-three-columns-with-the-max-of-a-fourth-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!