2016-04-26 47 views
0

我寫了下面的代碼來計算平均得分爲商業,現在我想在業務類別的TOP 2的業務,我嘗試添加HAVING COUNT(*) <= 2,但它不工作...如何獲得oracle sql中每個類別的前N個業務?

SELECT r.Bussiness_ID, b.Bussiness_Name, c.Catg_name, AVG(r.Rating) 
FROM review r,bussiness b, busn_catg c 
WHERE b.Bussiness_ID = r.Bussiness_ID 
AND b.Category = c.Catg_ID 
GROUP BY r.Bussiness_ID,b.Bussiness_Name, c.Catg_name 
ORDER BY c.Catg_name, AVG(r.Rating); 

回答

0
SELECT r.Bussiness_ID, b.Bussiness_Name, c.Catg_name, AVG(r.Rating) rt FROM review r,bussiness b, busn_catg c WHERE b.Bussiness_ID = r.Bussiness_ID AND b.Category = c.Catg_ID GROUP BY r.Bussiness_ID,b.Bussiness_Name, c.Catg_name ORDER BY c.Catg_name, rt desc; 
相關問題