首页 新闻 会员 周边

求一个分类查询SQL语句,在线等!

0
悬赏园豆:20 [已解决问题] 解决于 2009-06-24 17:15

 

如上图,是视图view_Articles的部分数据。

 

现在我想每个类别提取出两条最新的文章,请高手指点下SQL语句写法,或存储过程。

问题补充: 就没有人会么,伤心了。
Ropean的主页 Ropean | 初学一级 | 园豆:121
提问于:2009-06-15 18:06
< >
分享
最佳答案
0

使用排名函数:RANK()

 

使用排名函数:RANK()
create table ##Product
(
id
int,
ProductName
nvarchar(20),
ProductType
int
)

Create table ##ProductType(
id
int,
description
nvarchar(200)
)

insert into ##Product values (1,N'C#入门经典',1)
insert into ##Product values (2,N'C#高级编程',1)
insert into ##Product values (3,N'C#技术内幕',1)
insert into ##Product values (4,N'N70',2)
insert into ##Product values (5,N'N73',2)
insert into ##Product values (6,N'N95',2)

insert into ##ProductType values (1,N'图书')
insert into ##ProductType values (2,N'手机')


select * from (SELECT a.id, a.ProductName,b.description,
RANK()
OVER (PARTITION BY a.ProductType order by a.id desc) as RANK
FROM ##Product a join ##ProductType b on a.ProductType=b.id
) c
where c.RANK < 3
zengshunyou | 菜鸟二级 |园豆:345 | 2009-06-16 10:34
其他回答(2)
0

select * from view_Articles where id > max(id) -2 /

这个不行的话

select * from view_Articles where id > (select max(id) -2 from view_Articles )

Jack Lee | 园豆:210 (菜鸟二级) | 2009-06-15 22:23
0

你是按什么来分类的?而且又是通过什么知道他是最新的数据?

比如 表名: TableTest  字段:信息,分类,时间

那么实现语句是:

WITH TT AS
(SELECT [信息],rownum = row_number() over(partition by [分类] order by [时间] desc) FROM TableTest )

select [信息] from TT where rownum = 1 or rownum = 2

请参照:http://www.cnblogs.com/scorpioyyy/archive/2008/11/04/1325997.html

| 园豆:770 (小虾三级) | 2009-06-16 09:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册