sql 查询问题

mmkkuoi 2011-02-28 11:53:14
有一张表

id name count
1 aa 1
2 aa 2

3 bb 1
4 bb 2
5 bb 3

6 cc 1
7 cc 3

我希望得到的结果是
id name count
2 aa 2
5 bb 3
7 cc 3

就是的到count 最大的结果集 ,
如果后面增加一列 id name count (8,ee,1);

我希望得到的结果是
id name count
2 aa 2
5 bb 3
7 cc 3
8 ee 1
...全文
47 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
快溜 2011-03-01
  • 打赏
  • 举报
回复
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

/*
Andy-W 2011-03-01
  • 打赏
  • 举报
回复
Select id ,name, count From tb As a Where a.count=(Select Max(count) From tb Where name=a.name)
Andy-W 2011-03-01
  • 打赏
  • 举报
回复
id name count

Select id ,name, count
快溜 2011-03-01
  • 打赏
  • 举报
回复
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

/*
快溜 2011-03-01
  • 打赏
  • 举报
回复
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
-晴天 2011-03-01
  • 打赏
  • 举报
回复
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 行受影响)

*/

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧