Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92186453
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: Oracle

2008-04-09 09:58:21

   来源:赛迪网    作者:Alizze

在下面的示例中,表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

阅读(144) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~