简体   繁体   中英

SQL Group sort by date

I have a table that resembles the following:

id
type
created (date).

What I want is to return the most recently created item of each type. So it will return one item (most recent) for each type.

EG

id:1 type:A created:2011

id:2 type:A created:2008

id:3 type: B created:2009

id:4 type: B created:2010

This will return with record id 1 and 4.

this works using a self join

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM