日期:2014-05-18  浏览次数:20635 次

数据库统计问题
我有一个图书馆借阅的数据表,我想对该表进行处理,得出表中各本书借阅的排行

表中有:序号行(ID),借阅者卡号(USERCARDID),管理员卡号(AdminID),书籍的序列号(BookID),书籍名称(Name)

每借一次书都会生成一个新的行,

要的结果就是对书籍借阅进行统计,并排序,得出借阅次数的从高到低的排列,并且给出相应的书名。

------解决方案--------------------
SQL code
select name as 书籍名,count(1) as 借阅次数
from tb
group by name
order by 借阅次数 desc

------解决方案--------------------
SQL code
--应该按照书籍名称(Name)进行统计吧?
--sql 2000
select m.* , px = (select count(distinct cnt) from 
(
  select  Name , count(1) cnt from tb group by name
) n where n.cnt > m.cnt) from
(
  select  Name , count(1) cnt from tb group by name
) m
order by px

--sql 2005
select m.* , px = RANK () OVER(order by cnt desc) from
(
  select  Name , count(1) cnt from tb group by name
) m
order by px