我有一个图书馆借阅的数据表,我想对该表进行处理,得出表中各本书借阅的排行
表中有:序号行(ID),借阅者卡号(USERCARDID),管理员卡号(AdminID),书籍的序列号(BookID),书籍名称(Name)
每借一次书都会生成一个新的行,
要的结果就是对书籍借阅进行统计,并排序,得出借阅次数的从高到低的排列,并且给出相应的书名。
------解决方案--------------------
- SQL code
select name as 书籍名,count(1) as 借阅次数from tbgroup by nameorder by 借阅次数 desc
------解决方案--------------------
- SQL code
--应该按照书籍名称(Name)进行统计吧?--sql 2000select 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) morder by px--sql 2005select m.* , px = RANK () OVER(order by cnt desc) from( select Name , count(1) cnt from tb group by name) morder by px