首页 > 数据库技术 > 详细

【转】[sql server] 分组取最大值最小值常用sql

时间:2015-05-13 19:25:44      阅读:288      评论:0      收藏:0      [点我收藏+]

转自:http://blog.csdn.net/xys_777/article/details/5711128

--分组取最大最小常用sql
--测试环境
if OBJECT_ID(‘tb‘) is not null drop table tb;
go
create table tb(
 col1 int,
 col2 int,
 Fcount int)
insert into tb
select 11,20,1 union all
select 11,22,1 union all
select 11,23,2 union all
select 11,24,5 union all
select 12,39,1 union all
select 12,40,3 union all
select 12,38,4
go
--查询
--1
select * from tb t where Fcount=(select max(Fcount)from tb where col1=t.col1)
--2
select * from tb t where not exists(select 1 from tb where col1=t.col1 and Fcount>t.Fcount)    --效率要高很多(lui2015-5-13注释)
--结果
/*
col1        col2        Fcount
----------- ----------- -----------
12          38          4
11          24          5

*/

【转】[sql server] 分组取最大值最小值常用sql

原文:http://www.cnblogs.com/lj821022/p/4501121.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!