取出点击量最高的文章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 //类型名称
 
)

解决方案 »

  1.   

    本帖最后由 libin_ftsafe 于 2009-04-14 11:33:43 编辑
      

  2.   

    不好意思  第一个表的结构有点错误 修改后 doc表结构
    //文章表
    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) 是逻辑上存在的关系)
      

  3.   

    select top 100 *
    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
      

  4.   

    select top 100 *
    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
      

  5.   


    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