select * from 
(
  select top 5 * from 
      ( select sum(number) as amount,ISBN from tab_Order_detail group by ISBN )as tab order by amount desc) o 
      inner join 
      tab_bookinfo b 
      on o.ISBN=b.ISBN order by o.amount desc
( select sum(number) as amount,ISBN from tab_Order_detail group by ISBN )as tab order by amount desc) o 
      inner join 
      tab_bookinfo b 
      on o.ISBN=b.ISBN order by o.amount desc   做了一个inner join  内连接 得到一个表 (o inner join b on o.ISBN=b.ISBN)select top 5 * from    然后取前5行最后 select *   取全部结果

解决方案 »

  1.   

    sql="select * from (select top 5 * from ( select sum(number) as amount,ISBN from tab_Order_detail group by ISBN )as tab order by amount desc) o inner join tab_bookinfo b on o.ISBN=b.ISBN order by o.amount desc"首先是将表O和表b(tab_bookinfo )根据条件o.ISBN=b.ISBN联结起来,然后根据条件o.amount desc分组查询所有信息;可令select1=select top 5 * from ( select sum(number) as amount,ISBN from tab_Order_detail group by ISBN )as tab order by amount desc,则原语句可被替代为sql="select * from select1 o inner join tab_bookinfo b on o.ISBN=b.ISBN order by o.amount desc"select1即为表Oselect1又是怎么得到的呢?
    我们又可令select2=select sum(number) as amount,ISBN from tab_Order_detail group by ISBN ,则selec1是从表selec2中查询前5行所得到的;则select1可被替换为select1=select top 5 * from select2 as tab order by amount desc,其中又通过as tab将select1命名为tab;select2就好理解了。仅做参考,错误之处敬请批评指正~~~
      

  2.   

    select * from 
    (select top 5 * from
      ( select sum(number) as amount,ISBN from tab_Order_detail 
       group by ISBN )as tab
      order by amount desc
    ) o

      inner join tab_bookinfo b
     on o.ISBN=b.ISBN 
    order by o.amount desc
    蓝色部分,以ISBN 分组,求number之和,然后按number之和降序取得前五条记录集。
    然后和tab_bookinfo 连接,取出所有满足条件的纪录。
    条件是 ISBN相等,并amount 降序排序