2016-02-28 50 views
0

我有兩個表。排名函數總和

abc(CID(pk), cname,) 
order(order_id(pk), CID(fk), number_of_rentals) 

我想根據他們租用的電影數量來確定前10名客戶。

select 
    orders.cid,orders.no_rentals, abc.name, 
    rank() over (order by no_rentals desc) "rank" 
from abc 
inner join orders on orders.CID = abc.CID; 

我用這個查詢,但它不是通用的。如何使用此查詢在number_of_rentals上使用sum函數?

+0

的可能的複製[甲骨文SELECT TOP 10記錄](http://stackoverflow.com/questions/2498035/oracle-select-top-10-records) – MT0

回答

0
Select Top 10 
     orders.cid 
     , abc.name 
     , SUM(orders.no_rentals) TotalRentals 
     , rank() over (order by SUM(orders.no_rentals) desc) [rank] 
from abc 
inner join orders on orders.CID = abc.CID 
Group By orders.cid, abc.name 
Order By TotalRentals DESC 
+0

SELECT CID,總和(no_rentals)作爲總和 FROM訂單 group by cid,no_rentals order by no_rentals desc;這個查詢也給出了結果,但我如何獲取前10行使用此查詢..我正在使用oracle sql developer –

+0

您的查詢給出錯誤從關鍵字找不到預期的地方 –