sql server中分组查询后按排序顺序取每组第一条记录

时间:2022-09-16 12:13:18
有表a

字段如下  id    title    link_a    link_b
1, a, 2010a.jpg, 2010b.jpg
2, b, 2010a.jpg, 2010b.jpg
3, c, 2010a.jpg, 2010b.jpg
1, d, 2010a.jpg, 2010b.jpg
2, e, 2010a.jpg, 2010b.jpg
3, a, 2010a.jpg, 2010b.jpg
1, b, 2010a.jpg, 2010b.jpg
2, c, 2010a.jpg, 2010b.jpg
3, d, 2010a.jpg, 2010b.jpg
4, e, 2010a.jpg, 2010b.jpg

我想按id分组后  把每组的第一条记录取出来
即id是1的取一条  id是2的取一条  id是3的取一条  id是4的取一条。。。
求一sql语句

6 个解决方案

#1


何为第一条?

#2


create table tb(id int,title nvarchar(10),link_a nvarchar(20),link_b nvarchar(20))
insert into tb select 1,'a','2010a.jpg','2010b.jpg'
insert into tb select 2,'b','2010a.jpg','2010b.jpg'
insert into tb select 3,'c','2010a.jpg','2010b.jpg'
insert into tb select 1,'d','2010a.jpg','2010b.jpg'
insert into tb select 2,'e','2010a.jpg','2010b.jpg'
insert into tb select 3,'a','2010a.jpg','2010b.jpg'
insert into tb select 1,'b','2010a.jpg','2010b.jpg'
insert into tb select 2,'c','2010a.jpg','2010b.jpg'
insert into tb select 3,'d','2010a.jpg','2010b.jpg'
insert into tb select 4,'e','2010a.jpg','2010b.jpg'
go
select * from tb a where not exists(select 1 from tb where id=a.id and title<a.title)
/*
id          title      link_a               link_b
----------- ---------- -------------------- --------------------
1           a          2010a.jpg            2010b.jpg
2           b          2010a.jpg            2010b.jpg
3           a          2010a.jpg            2010b.jpg
4           e          2010a.jpg            2010b.jpg

(4 行受影响)

*/
go
drop table tb

#3


引用 1 楼 qianjin036a 的回复:
何为第一条?

就是说按id分组后   每组数据的第一条数据

#4


CREATE TABLE TB
(
   id int,
   title varchar(1),
   link_a varchar(10),
   link_b varchar(10)
)
insert into tb
select 1,'a','2010a.jpg','2010b.jpg' union all
select 2,'b','2010a.jpg','2010b.jpg' union all
select 3,'c','2010a.jpg','2010b.jpg' union all
select 1,'d','2010a.jpg','2010b.jpg' union all
select 2,'e','2010a.jpg','2010b.jpg' union all
select 3,'a','2010a.jpg','2010b.jpg' union all
select 1,'b','2010a.jpg','2010b.jpg' union all
select 2,'c','2010a.jpg','2010b.jpg' union all
select 3,'d','2010a.jpg','2010b.jpg' union all
select 4,'e','2010a.jpg','2010b.jpg'

;with cte as
(
  select ROW_NUMBER()over(order by getdate())as rn,* from TB
)
select * from cte a where not exists(select 1 from cte b where a.id=b.id and a.rn>b.rn )


/*
-------------------
rn id title link_a link_b
1 1 a 2010a.jpg 2010b.jpg
2 2 b 2010a.jpg 2010b.jpg
3 3 c 2010a.jpg 2010b.jpg
10 4 e 2010a.jpg 2010b.jpg
*/

#5


引用 2 楼 qianjin036a 的回复:
SQL code
create table tb(id int,title nvarchar(10),link_a nvarchar(20),link_b nvarchar(20))
insert into tb select 1,'a','2010a.jpg','2010b.jpg'
insert into tb select 2,'b','2010a.jpg','2010b.jpg'
……

如果title的内容一样 id的内容也一样的话就不行了

#6


引用 5 楼 a12mao90 的回复:
如果title的内容一样 id的内容也一样的话就不行了

用临时表.
select * , px = identity(int,1,1) into tmp from tb
select t.* from tmp where not exists(select 1 from tmp where id = t.id and (name < t.name or (name = t.name and px < t.px)) )

#1


何为第一条?

#2


create table tb(id int,title nvarchar(10),link_a nvarchar(20),link_b nvarchar(20))
insert into tb select 1,'a','2010a.jpg','2010b.jpg'
insert into tb select 2,'b','2010a.jpg','2010b.jpg'
insert into tb select 3,'c','2010a.jpg','2010b.jpg'
insert into tb select 1,'d','2010a.jpg','2010b.jpg'
insert into tb select 2,'e','2010a.jpg','2010b.jpg'
insert into tb select 3,'a','2010a.jpg','2010b.jpg'
insert into tb select 1,'b','2010a.jpg','2010b.jpg'
insert into tb select 2,'c','2010a.jpg','2010b.jpg'
insert into tb select 3,'d','2010a.jpg','2010b.jpg'
insert into tb select 4,'e','2010a.jpg','2010b.jpg'
go
select * from tb a where not exists(select 1 from tb where id=a.id and title<a.title)
/*
id          title      link_a               link_b
----------- ---------- -------------------- --------------------
1           a          2010a.jpg            2010b.jpg
2           b          2010a.jpg            2010b.jpg
3           a          2010a.jpg            2010b.jpg
4           e          2010a.jpg            2010b.jpg

(4 行受影响)

*/
go
drop table tb

#3


引用 1 楼 qianjin036a 的回复:
何为第一条?

就是说按id分组后   每组数据的第一条数据

#4


CREATE TABLE TB
(
   id int,
   title varchar(1),
   link_a varchar(10),
   link_b varchar(10)
)
insert into tb
select 1,'a','2010a.jpg','2010b.jpg' union all
select 2,'b','2010a.jpg','2010b.jpg' union all
select 3,'c','2010a.jpg','2010b.jpg' union all
select 1,'d','2010a.jpg','2010b.jpg' union all
select 2,'e','2010a.jpg','2010b.jpg' union all
select 3,'a','2010a.jpg','2010b.jpg' union all
select 1,'b','2010a.jpg','2010b.jpg' union all
select 2,'c','2010a.jpg','2010b.jpg' union all
select 3,'d','2010a.jpg','2010b.jpg' union all
select 4,'e','2010a.jpg','2010b.jpg'

;with cte as
(
  select ROW_NUMBER()over(order by getdate())as rn,* from TB
)
select * from cte a where not exists(select 1 from cte b where a.id=b.id and a.rn>b.rn )


/*
-------------------
rn id title link_a link_b
1 1 a 2010a.jpg 2010b.jpg
2 2 b 2010a.jpg 2010b.jpg
3 3 c 2010a.jpg 2010b.jpg
10 4 e 2010a.jpg 2010b.jpg
*/

#5


引用 2 楼 qianjin036a 的回复:
SQL code
create table tb(id int,title nvarchar(10),link_a nvarchar(20),link_b nvarchar(20))
insert into tb select 1,'a','2010a.jpg','2010b.jpg'
insert into tb select 2,'b','2010a.jpg','2010b.jpg'
……

如果title的内容一样 id的内容也一样的话就不行了

#6


引用 5 楼 a12mao90 的回复:
如果title的内容一样 id的内容也一样的话就不行了

用临时表.
select * , px = identity(int,1,1) into tmp from tb
select t.* from tmp where not exists(select 1 from tmp where id = t.id and (name < t.name or (name = t.name and px < t.px)) )