从数据库中选择不同的第一行

时间:2011-06-20 14:26:44

标签: postgresql distinct

number      | time 

421112233444 | 1304010250
 421112233444 | 1304272979
 421001122333 | 1303563263
 421112233444 | 1300217115
 421001122333 | 1303649310

我需要从第二行返回唯一具有最小编号的第一行,如下所示:

421112233444 | 1300217115
421001122333 | 1303563263

任何想法?

我尝试SELECT ph.number,mo.time from(select distinct(number)from table)ph,table mo其中mo.number = ph.number;

但它返回两个唯一身份:

421112233444 | 1304010250
 421112233444 | 1304272979
 421001122333 | 1303563263
 421112233444 | 1300217115
 421001122333 | 1303649310

2 个答案:

答案 0 :(得分:3)

您可以使用GROUP BY执行此操作:

SELECT number, MIN(time)
FROM table
GROUP BY number
ORDER BY number DESC
LIMIT 2

答案 1 :(得分:0)

SELECT ph.number, min(mo.time) from (select distinct(number) from table) ph, table mo group by ph.number