【赛迪网-it技术报道】在下面的示例中,表classname中有如下分类:
具体示例:
classid classname
1 衣服
2 裤子
5 帽子
10 鞋子
表productinfo有如下记录:
productid productname parentid clicknum
1 男士衣服 1 90 --衣服类别中这条记录的点击率最高
2 女士衣服 1 80
3 男士裤子 2 70
4 女士裤子 2 90
--裤子类别中这条记录点击率最高
5 男士帽子 5 15
6 女士帽子 5 30
帽子类别中这条点击率最高
7 男士鞋子 10 65
--鞋子类别中这条点击率最高
8 女士鞋子 10 52
9 女士鞋子1 10 54
现在我们要求分别把衣服,裤子,帽子,鞋子这些类别中点击率最高的一条记录找出来,然后再降序排列,结果如下:
productid productname clicknum
1 男士衣服 90
4 女士裤子 90
7 男士鞋子 65
6 女士帽子 30
实现方法:
declare @temp table
(
productid int,
productname nvarchar(30),
clicknum int
)
declare @classid int
declare cursor_classid cursor
for
select classid from dbo.classname
open cursor_classid
fetch next from cursor_classid into @classid
--0 表示 fetch 语句成功
while @@fetch_status=0
begin
insert into @temp
select top 1 productid,productname,clicknum from dbo.productinfo
where parentid = @classid
order by clicknum desc
fetch next from cursor_classid into @classid
end
close cursor_classid
deallocate cursor_classid
select * from @temp order by clicknum desc
闽公网安备 35060202000074号