开发者

Select top record from group

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜