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

SQL 查询每组的第一条记录

时间:2015-09-19 22:33:22      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

CREATE TABLE [dbo].[test1](
    [program_id] [int] NULL,
    [person_id] [int] NULL
) ON [PRIMARY]

/*查询每组分组中第一条记录*/
select * from test1 as a
where a.person_id in 
( select  top 1 person_id from test1
where program_id = a.program_id);

select * from (  
   select ROW_NUMBER() over(partition by program_id order by person_id) as num,* from test1) as t
   where num=1

 

SQL 查询每组的第一条记录

标签:

原文地址:http://www.cnblogs.com/changshuo/p/4822260.html

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