会数据库的帮帮忙啊....

来源:百度知道 编辑:UC知道 时间:2024/06/23 14:11:32
请教在数据库中怎样将一组数据中的相同名称的每10个最小值查找或者列出来?
例如给了你大量厂家名称和他们不同品种的货物价格,查找出当中每家厂家不同品种中10个最小的数值...
企业名称 分类 器械名 规格 价格,给出的是这样的表.要求查询出相同企业,同一分类,同一器械名,全部规格中最小的10个价格

select * from 表 a
where not exists(select 1 from 表 where 单据=a.单据 and 日期<a.日期)

想来想去,还是没想出一条语句能解决的办法,希望高手指点,学习下。本人简单写了个过程,在查询分析器中貌似可行:
假定表名是tt2,结构如下
declare @tt table(
企业名称 varchar(10),
分类 varchar(10),
器械名 varchar(10),
规格 varchar(10),
价格 numeric(9,2))

declare @企业名称 varchar(10)
declare @分类 varchar(10)
declare @器械名 varchar(10)
declare @规格 varchar(10)
declare @价格 numeric(9,2)
declare @index numeric(9)
declare @max numeric(9)

set @index = 0 --循环变量
set @max = 10 --前10个
declare c1 cursor for
select 企业名称,分类,器械名,规格,价格 from tt2 order by 企业名称,分类,器械名,价格
open c1
fetch next from c1 into @企业名称,@分类,@器械名,@规格,@价格
while (@@fetch_status = 0)
begin
if not exists (select 1 from @tt where 企业名称 = @企业名称 and 分类 = @分类 and 器械名 = @器械名) set @index = 0
set @index = @index + 1
if @index <= @max insert into @tt(企业名称,分类,器械名,规格,价格) values (@企