2011-12-08 35 views
1

我有一个类似于下面的表格:SQL集团按日期排序

id 
type 
created (date). 

我想是返回每种类型的最近创建的项目。所以它会为每种类型返回一个项目(最新)。

E.G.

id:1 type:A created:2011

id:2 type:A created:2008

id:3 type: B created:2009

id:4 type: B created:2010

这将记录id 1和4

+1

这种类型的(经常被问到的)查询有一个标签:**'[most-n-per-group]'**。点击标签或右侧的** Related **标题下。 –

回答

1

这个工程使用自返回加入

select T1.* from table t1 LEFT JOIN table t2 
ON t1.type = t2.type and t1.created < t2.created 
where t2.id is null