按名称和最受欢迎的标题排序 - Oracle

时间:2012-07-22 05:50:00

标签: oracle11g

我有这个查询

select movie_name, user_name
from movie natural join movie_queue natural join users
group by (movie_name, user_name)
order by movie_name;


这会删除类似下面的列表:

 MOVIE_NAME                                              USER_NAME              
------------------------------------------------------- ------------------------
E.T. the Extra-Terrestrial                              Cris                     
E.T. the Extra-Terrestrial                              Elizabeth                
E.T. the Extra-Terrestrial                              John                     
E.T. the Extra-Terrestrial                              Mary                     
E.T. the Extra-Terrestrial                              Peter                    
Indiana Jones and the Kingdom of the Crystal Skull      Elizabeth                
Indiana Jones and the Kingdom of the Crystal Skull      John                     
Indiana Jones and the Kingdom of the Crystal Skull      Mary                     
Jurassic Park                                           Elizabeth                
Jurassic Park                                           John                     
Jurassic Park                                           Mary                     
Jurassic Park                                           Peter                    
Signs                                                   Elizabeth                
The Sixth Sense                                         Mary                     
Unbreakable                                             John                     
Unbreakable                                             Mary                     
Unbreakable                                             Peter                    
War of the Worlds                                       Elizabeth                
War of the Worlds                                       John                     
War of the Worlds                                       Mary                     
War of the Worlds                                       Peter  


问题是如何按行流行度和按电影名称订购?
我目前正在通过movie_name订购列表,但我也想从中订购它们 最不受欢迎的电影中最受欢迎的电影。


期望的结果:
结果是, “E.T. the Extra-Terrestrial”行应该首先出现,因为它在桌子上出现的次数更多,“The Sixth Sense”应该出现在“Signs”之后的最后。因为最后一部电影只出现一次,但是S出现在T之前。

我尝试添加这个: 按计数(movie_name),movie_name;

排序

但我仍然没有得到我想要的结果。 最后,电影应该像这样组织

这就是我希望它们的组织方式:从更受欢迎到不受欢迎,按字母顺序排列

 MOVIE_NAME                                              USER_NAME              
------------------------------------------------------- ------------------------
E.T. the Extra-Terrestrial                              Cris                     
E.T. the Extra-Terrestrial                              Elizabeth                
E.T. the Extra-Terrestrial                              John                     
E.T. the Extra-Terrestrial                              Mary                     
E.T. the Extra-Terrestrial                              Peter  
Jurassic Park                                           Elizabeth                
Jurassic Park                                           John                     
Jurassic Park                                           Mary                     
Jurassic Park                                           Peter 
War of the Worlds                                       Elizabeth                
War of the Worlds                                       John                     
War of the Worlds                                       Mary                     
War of the Worlds                                       Peter   
Indiana Jones and the Kingdom of the Crystal Skull      Elizabeth                
Indiana Jones and the Kingdom of the Crystal Skull      John                     
Indiana Jones and the Kingdom of the Crystal Skull      Mary  
Unbreakable                                             John                     
Unbreakable                                             Mary                     
Unbreakable                                             Peter 
Signs                                                   Elizabeth                
The Sixth Sense                                         Mary                 

提前致谢

2 个答案:

答案 0 :(得分:0)

您可以为order by子句指定多个列。此外,您甚至可以混合使用desc。

在你的情况下,它会是这样的:

select movie_name, user_name, count(movie_name) popularity 
from movie natural join movie_queue natural join users
group by (movie_name, user_name)
order by popularity, movie_name;

另见:http://psoug.org/reference/orderby.html

答案 1 :(得分:0)

试试这样:

select movie_name, user_name
from movie natural join movie_queue natural join users
order by count(*) over (partition by movie_name) ,movie_name;
相关问题