码迷,mamicode.com
首页 > 数据库 > 详细

MSSQL 分组后取每组第一条(group by order by)

时间:2015-05-05 12:05:37      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

查询中经常遇到这种查询,分组后取每组第一条。分享下一个SQL语句:

 

--根据 x 分组后、根据 y 排序后取第一条
select * from (
 select ROW_NUMBER() over(partition by x order by y desc) RowNum
   ,testTable.*
   from testTable) as t1  where RowNum = 1


注:我使用MS SQL 08 R2

MSSQL 分组后取每组第一条(group by order by)

标签:

原文地址:http://www.cnblogs.com/moretry/p/4478705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!