设为首页 加入收藏

TOP

获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)
2015-07-24 11:16:00 来源: 作者: 【 】 浏览:3
Tags:获取 分组 后取某 段最 大一 记录 每个 类别 最大
获取分组后取某字段最大一条记录
方法一:(效率最高)
select * from test as a
where typeindex = (select max(b.typeindex)
from test as b
where a.type = b.type );


方法二:(效率次之)
select
a.* from test a,
(select type,max(typeindex) typeindex from test group by type) b
where a.type = b.type and a.typeindex = b.typeindex order by a.type




方法三:
select a.* from test a inner join (select type , max(typeindex) typeindex from test group by type) b on a.type = b.type and a.typeindex = b.typeindex order by a.type


方法四:(效率最低)
select * from
(
select *,ROW_NUMBER() OVER(PARTITION BY type ORDER BY typeindex DESC) as num
from test
) t
where t.num = 1
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇对数据库表中的某一字段去重分组.. 下一篇WAS集群系列(3):集群搭建:步..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Shell脚本:Linux Sh (2025-12-25 05:50:11)
·VMware虚拟机安装Lin (2025-12-25 05:50:08)
·Linux学习教程,Linu (2025-12-25 05:50:06)
·工业机器人TCP校准中 (2025-12-25 05:19:17)
·opc 通讯协议与 TCP (2025-12-25 05:19:15)