首先给叙述一下,有一个表,这个表命名为book,假设这个表只有三列 title(书名)、种类(category)、每本书的价格(retail),请问一下,那么销售种类最高的是什么种类,怎么求?
除了这种方法
select category from book 
group by category 
having max(retail)=
(select max(max(retail)) from book
group by category);
之外,为什么下面这种方法运行不出结果
select category from book
 group by category 
having max(retail)>=all(select max(retail) from book group by category);
求解决!

解决方案 »

  1.   


    select book.category from book,
    (select category,max(retail) retail from book group by category) t
    where book.category=t.category;
      

  2.   

    感觉楼上的方法不错啊,我想明白为什么我用all的那个方法不行???????
      

  3.   

    比如说吧,有一个computer类的书,这里面有java售价2,c售价3。还有一个是cooking的书,有a售价4,b售价2吧,我就想问一下,通过子查询怎么知道价格最高的是哪一类!
      

  4.   

    select category from book
     group by category  
    having max(retail)>=all(select max(retail) from book group by category);
    这种方法应该可以达到LZ要的结果吧。不过感觉是不是太麻烦了。
    直接
     select title,category,retail from (
     select title,category,retail,row_number() over(order by  retail desc) rn 
     from book) where rn='1'
    或者
    select * from (select  title,category,retail from book order by retail  desc)
    where rownum=1
      

  5.   

    楼主的方法有误,你是求种类的售价,而不是单本书的最高价格,需要先sum,因为一个种类里面可能包含很多本书,然后再max,6楼的方法更是错的,楼主试一下这样写:select category from
    (select category,sum(retail) retail from book group by category)
    group by category
    having max(retail)=(select max(sum(retail)) from book group by category);至于楼主的第二个方法其实是没有问题的
    select category from
    (select category,sum(retail) retail from book group by category)
    group by category
    having max(retail)>=all(select max(sum(retail)) from book group by category);
      

  6.   

    楼主的方法错了,你这样不能求出销售种类最高的分类,如果要做到销售种类最高,首先要sum,因为一个种类里面可能包含很多本书,然后再max,你试一下这个方法select category from
    (select category, sum(retail) retail from book  group by category)
    group by category  
    having max(retail)=
    (select max(sum(retail)) from book group by category);楼主的方法二的思路没错,我修改了一下
    select category from
    (select category, sum(retail) retail from book  group by category)
    group by category  
    having max(retail)>=
    all(select max(sum(retail)) from book group by category);