0

我有这个查询

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 排序?
我目前正在按电影名称对列表进行排序,但我也想将它们从最受欢迎的电影排序到不太受欢迎的电影。


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

我尝试添加:按计数排序(电影名称),电影名称;

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

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

 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                 

提前致谢

4

2 回答 2

0

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

在您的情况下,它将类似于:

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

于 2012-07-22T06:04:04.410 回答
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;
于 2012-07-22T08:29:12.163 回答