求每组前2名,你有几种方法?(sql server2000+sql server2005)
2004-08-23 11:55
316 查看
create table abc(
i nvarchar(10),
ii int,
iii int,
iiii int,
price money)
go
insert into abc
select 'b',1,2,1,11
union all
select 'b',211,2,1,211
union all
select 'a',21,2,1,311
union all
select 'd',41,42,1,411
union all
select 'd',41,42,1,511
union all
select 'd',41,42,1,611
union all
select 'e',1,2,1,11
union all
select 'e',71,2,1,31
union all
select 'e',61,2,1,911
union all
select 'e',771,2,1,1
go
要求的结果是:
以i分组,求每组price最大的前2条记录
i ii iii iiii price
---------- ----------- ----------- ----------- ---------------------
a 21 2 1 311.0000
b 1 2 1 11.0000
b 211 2 1 211.0000
d 41 42 1 511.0000
d 41 42 1 611.0000
e 71 2 1 31.0000
e 61 2 1 911.0000
1.
select *
from abc a
where (
select count(*) from abc b
where a.i=b.i and b.price>a.price)<2
order by i,price
连接查询,判断数量
2.
select i,ii,iii,iiii,price
from (
select (select isnull(sum(1),0)+1 from abc b where a.i=b.i and a.price<b.price) ids,*
from abc a) tem
where ids<3
order by i,price
生产一个内部表,给内部表tem中的每一组排序,并把排序号放入新列ids中
3.
declare @looptime int
declare @count int
declare @i nvarchar(10)
/*定义表变量@abc,和表ABC中的所有列类型相同*/
declare @abc table(
i nvarchar(10),
ii int,
iii int,
iiii int,
price money)
declare @tem table(
ids int identity,
class nvarchar(10))
/*把表ABC中的所有组全部查询出来,暂时存在表变量@tem中*/
insert into @tem(class)
select i
from abc
group by i
/*求出表变量@tem中行数量*/
select @count=@@rowcount
/*循环变量@looptime赋初值=1*/
select @looptime=1
while(@looptime<=@count)
begin
/*将每组名赋值到变量@i*/
select @i=class
from @tem
where ids=@looptime
/*将每组前2名插入到表变量@abc中*/
insert into @abc
select top 2 *
from abc
where i=@i
order by price desc
/*循环变量@looptime累加1*/
select @looptime=@looptime+1
end
/*显示结果*/
select *
from @abc
order by i,price
4.
用游标来处理
方法和我的第3种方法类似,大家可以自己试试
5.利用sql server2005排名函数
SELECT *
FROM ( SELECT i ,
ii ,
iii ,
iiii ,
price ,
de = ROW_NUMBER() OVER ( PARTITION BY i ORDER BY [price] DESC )
FROM abc
) AS bb
WHERE de <= 2
我共写了5种,1到4都可以在sql2000下运行,5必须在sql2005运行
不知道大家还有什么其他好方法,可以交流,谢谢。
i nvarchar(10),
ii int,
iii int,
iiii int,
price money)
go
insert into abc
select 'b',1,2,1,11
union all
select 'b',211,2,1,211
union all
select 'a',21,2,1,311
union all
select 'd',41,42,1,411
union all
select 'd',41,42,1,511
union all
select 'd',41,42,1,611
union all
select 'e',1,2,1,11
union all
select 'e',71,2,1,31
union all
select 'e',61,2,1,911
union all
select 'e',771,2,1,1
go
要求的结果是:
以i分组,求每组price最大的前2条记录
i ii iii iiii price
---------- ----------- ----------- ----------- ---------------------
a 21 2 1 311.0000
b 1 2 1 11.0000
b 211 2 1 211.0000
d 41 42 1 511.0000
d 41 42 1 611.0000
e 71 2 1 31.0000
e 61 2 1 911.0000
1.
select *
from abc a
where (
select count(*) from abc b
where a.i=b.i and b.price>a.price)<2
order by i,price
连接查询,判断数量
2.
select i,ii,iii,iiii,price
from (
select (select isnull(sum(1),0)+1 from abc b where a.i=b.i and a.price<b.price) ids,*
from abc a) tem
where ids<3
order by i,price
生产一个内部表,给内部表tem中的每一组排序,并把排序号放入新列ids中
3.
declare @looptime int
declare @count int
declare @i nvarchar(10)
/*定义表变量@abc,和表ABC中的所有列类型相同*/
declare @abc table(
i nvarchar(10),
ii int,
iii int,
iiii int,
price money)
declare @tem table(
ids int identity,
class nvarchar(10))
/*把表ABC中的所有组全部查询出来,暂时存在表变量@tem中*/
insert into @tem(class)
select i
from abc
group by i
/*求出表变量@tem中行数量*/
select @count=@@rowcount
/*循环变量@looptime赋初值=1*/
select @looptime=1
while(@looptime<=@count)
begin
/*将每组名赋值到变量@i*/
select @i=class
from @tem
where ids=@looptime
/*将每组前2名插入到表变量@abc中*/
insert into @abc
select top 2 *
from abc
where i=@i
order by price desc
/*循环变量@looptime累加1*/
select @looptime=@looptime+1
end
/*显示结果*/
select *
from @abc
order by i,price
4.
用游标来处理
方法和我的第3种方法类似,大家可以自己试试
5.利用sql server2005排名函数
SELECT *
FROM ( SELECT i ,
ii ,
iii ,
iiii ,
price ,
de = ROW_NUMBER() OVER ( PARTITION BY i ORDER BY [price] DESC )
FROM abc
) AS bb
WHERE de <= 2
我共写了5种,1到4都可以在sql2000下运行,5必须在sql2005运行
不知道大家还有什么其他好方法,可以交流,谢谢。
相关文章推荐
- SQL Server2005的几种分页方法
- 浅谈SQL Server2005的几种分页方法
- 浅谈SQL Server2005的几种分页方法
- 浅谈SQL Server2005的几种分页方法
- 分组数据后取每组的前N条的几种方法
- 浅谈SQL Server2005的几种分页方法
- Jquery取得iframe中元素的几种方法
- 遍历map的几种方法
- 几种常用的C#排序方法简介
- 多线程的几种实现方法详解
- ASP.Net中MD5和SHA1加密的几种方法
- C#执行Javascript代码的几种方法总结
- Ubuntu 几种常见乱码解决方法
- MyEclipse安装插件的几种方法 转
- Linux 技巧:让进程在后台可靠运行的几种方法
- 创建滚动进度条的几种方法
- Android系列之Intent传递对象的几种实例方法
- 对.NET中导出数据到EXCEL的几种方法探讨
- JSON几种转换方法
- Nginx几种常见实现301重定向方法上的区别