开发者

Select top record from group

开发者 https://www.devze.com 2023-01-19 17:55 出处:网络
How to from this table NameCode ABCCode1 DEFCode1 GHICode2 JKLCode2 OMNCo开发者_如何学编程de3 get this result:

How to from this table

Name  Code
ABC     Code1
DEF     Code1
GHI     Code2
JKL     Code2
OMN     Co开发者_如何学编程de3

get this result:

Name  Code
ABC   Code1
GHI   Code2
OMN   Code3

Is there any simple solution?


SELECT MIN(Name),Code
FROM Table
GROUP BY Code


I considered that you want your results to be alphabetical.

SELECT
         MIN(Name), Code
FROM 
         MyTable 
GROUP BY 
         Code


SELECT MIN(c1), c2 FROM table GROUP BY c2 ORDER BY c2 ASC;


if your "TOP" is defined by "first in alphabtetical order", you could simply use a simple group by clause with the MIN aggregation.

SELECT MIN(Col1), Col2 FROM table GROUP BY Col2

otherwise, you will need another column, such as an incrementing uniqueid or a creation date.

0

精彩评论

暂无评论...
验证码 换一张
取 消