SQL 查询每组的第一条记录

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

 

posted @ 2015-09-19 21:16  苦逼IT男  阅读(669)  评论(0编辑  收藏  举报