下面的一个归类题,一时想不通,求大虾!
首先给叙述一下,有一个表,这个表命名为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);
求解决!
------解决方案--------------------没看明白你的意思,把你要的结果列出来吧
------解决方案--------------------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
------解决方案--------------------楼主的方法有误,你是求种类的售价,而不是单本书的最高价格,需要先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);