2012-07-22 50 views
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排序?
我目前正在通過movie_name訂購清單,但我也想從 最受歡迎的電影訂購到不太受歡迎的電影。


期望的結果:
結果, 「ET外星人」行應該首先出現,因爲它出現在表次數多了,「第六感」應該最後出現在「符號」之後。因爲兩部最後的電影都只出現一次,但S在T之前。

我試着添加這個: order by count(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     

感謝

回答

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

+0

@Burhan,謝謝!我明白了,我可以有多列,我試圖count(movie_name)作爲第二列,但我仍然沒有得到我想要的結果。我需要它把最受歡迎的電影放在第一位(流行是基於更多的行)。 – Cristy 2012-07-22 23:10:39

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; 
+0

謝謝!這工作! – Cristy 2012-07-22 23:20:57