[英]SQL Group sort by date
我有一个类似于以下表格:
id
type
created (date).
我想要的是返回每种类型的最新创建的项目。 因此,它将为每种类型返回一个项目(最新)。
例如
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。
这可以使用自我联接
select T1.* from table t1 LEFT JOIN table t2
ON t1.type = t2.type and t1.created < t2.created
where t2.id is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.