22,209
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,name varchar(10),cnt int)
insert into tb select 1,'aa',1
insert into tb select 2,'aa',2
insert into tb select 3,'bb',1
insert into tb select 4,'bb',2
insert into tb select 5,'bb',3
insert into tb select 6,'cc',1
insert into tb select 7,'cc',3
insert into tb select 8,'ee',1
select id,name,cnt
from (select row_number over(partition by name order by cnt desc) as no,*
from tb) a where a.no=1
/*
id name cnt
----------- ---------- -----------
2 aa 2
5 bb 3
7 cc 3
8 ee 1
/*
create table tb(id int,name varchar(10),cnt int)
insert into tb select 1,'aa',1
insert into tb select 2,'aa',2
insert into tb select 3,'bb',1
insert into tb select 4,'bb',2
insert into tb select 5,'bb',3
insert into tb select 6,'cc',1
insert into tb select 7,'cc',3
insert into tb select 8,'ee',1
select id,name,cnt
from (select row_number over(parttion by name order by cnt desc) as no,*
from tb) a where a.no=1
/*
id name cnt
----------- ---------- -----------
2 aa 2
5 bb 3
7 cc 3
8 ee 1
/*
select id,name,[count]
from (select row_number over(parttion by name order by [count] desc) as no,*
from tb) a where a.no=1
create table tb(id int,name varchar(10),cnt int)
insert into tb select 1,'aa',1
insert into tb select 2,'aa',2
insert into tb select 3,'bb',1
insert into tb select 4,'bb',2
insert into tb select 5,'bb',3
insert into tb select 6,'cc',1
insert into tb select 7,'cc',3
insert into tb select 8,'ee',1
go
select * from tb a where not exists(select 1 from tb where name=a.name and cnt>a.cnt)
go
drop table tb
/*
id name cnt
----------- ---------- -----------
2 aa 2
5 bb 3
7 cc 3
8 ee 1
(4 行受影响)
*/