分组去最大值的问题

feixian49 2010-06-13 10:23:20
我有一个表
ID Type Point
1 1 20
2 1 30
3 2 10
4 2 15
5 2 15

根据 Type 分类取最大的 Point,若 Point 相同取第一位
结果:
2 1 30
4 2 15
请问怎么写这个 Sql 语句?在线等。
...全文
78 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
宇峰科技 2010-06-13
  • 打赏
  • 举报
回复
学习。。。
rmljoe 2010-06-13
  • 打赏
  • 举报
回复
-- 建模拟数据
create table tb(ID int, Type int, Point int)
insert into tb select 1, 1, 20
union all select 2, 1, 30
union all select 3, 2, 10
union all select 4, 2, 15
union all select 5, 2, 15

-- 测试
select
*
from tb a
where not exists(select 1 from tb b where a.type = b.type and (a.point < b.point or (a.point = b.point and a.id > b.id)))

-- 结果
/*
ID Type Point
----------- ----------- -----------
2 1 30
4 2 15

(2 行受影响)
*/


我前几天刚才发贴问过,呵呵
xman_78tom 2010-06-13
  • 打赏
  • 举报
回复

select ID, Type, Point from
(select ID, Type, Point, row_number() over (partition by Type order by Point desc, ID) rid from tb) t
where rid=1;
htl258_Tony 2010-06-13
  • 打赏
  • 举报
回复
select * from tb t where id=(select max(id) from tb where type=t.type)
shixixi1987 2010-06-13
  • 打赏
  • 举报
回复
select max(Point) from table1 group by Type having count(Type)>1
albert_sky 2010-06-13
  • 打赏
  • 举报
回复

selct id,type,max(point) from T A where (select top 1 id from T where type=A.type order by point desc)
group by id,type

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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