SQL 分组后按照排序取第一个
时间:2010-09-14 来源:为什么??
因为用到了将Sql某个表中分组后再按照某种分类取第一个,找了一下,还真有相关的~注row_number()函数在上sql2005有效
代码
SELECT ID,
media_name,
media_type,
rownum,create_date
FROM (select media_id,resource_name ,media_type,create_date, row_number() over(partition by media_type order by create_date desc)
as rowNum from comm_media_info) k where k.rowNum=1
media_name,
media_type,
rownum,create_date
FROM (select media_id,resource_name ,media_type,create_date, row_number() over(partition by media_type order by create_date desc)
as rowNum from comm_media_info) k where k.rowNum=1
相关阅读 更多 +