取出点击量最高的文章100篇,属于同一个类型的文章不超过5篇,怎么写?最好 用mssql 语句写法能不能查询出 不同类型的文章点击量排行表结构如下://文章表
create table doc
(
id int identity(1,1) not null primary key ,
title varchar(255) not null ,//标题
state bit not null, //状态
coutent nvarchar(1000) //内容
cid int //外键 没有理上的外键关系 引用 class(Column_ID) 是逻辑上存在的关系
doc_click int not null default 0 //点击数
)//文章点击数表
create table doc_count
(
id int identity(1,1) not null primary key,
doc_id int not null ,//外键 没有建立物理上的外键关系 引用 doc(id) 是逻辑上存在的关系
doc_click int not null default 0 //点击数
)//文章类型表
create table class
(
Column_ID int identity(1,1) primary key,
Column_Name varchar(50) not null //类型名称
)
create table doc
(
id int identity(1,1) not null primary key ,
title varchar(255) not null ,//标题
state bit not null, //状态
coutent nvarchar(1000) //内容
cid int //外键 没有理上的外键关系 引用 class(Column_ID) 是逻辑上存在的关系
doc_click int not null default 0 //点击数
)//文章点击数表
create table doc_count
(
id int identity(1,1) not null primary key,
doc_id int not null ,//外键 没有建立物理上的外键关系 引用 doc(id) 是逻辑上存在的关系
doc_click int not null default 0 //点击数
)//文章类型表
create table class
(
Column_ID int identity(1,1) primary key,
Column_Name varchar(50) not null //类型名称
)
//文章表
create table doc
(
id int identity(1,1) not null primary key ,
title varchar(255) not null ,//标题
state bit not null, //状态
coutent nvarchar(1000) //内容
cid int //外键 没有理上的外键关系 引用 class(Column_ID) 是逻辑上存在的关系)
from (select *,cnt=row_number() over (partition by cid order by doc_click desc)
from doc as d join class as c on d.cid=Column_ID ) t
where cnt<=5
order by doc_click desc
from (select *,cnt=row_number() over (partition by cid order by doc_click desc)
from doc as d join class as c on d.cid=Column_ID ) t
where cnt<=5
order by doc_click desc
select top 100 *
from (select *,cnt=row_number() over (partition by cid order by doc_click desc)
from doc as d join doc_count as c on c.doc_id=d.id) t
where cnt<=5
order by doc_click desc