如何在oracle sql的每个类别中获得前N个业务?

时间:2016-04-26 17:10:21

标签: sql greatest-n-per-group

我编写以下代码来计算业务的平均评级,现在我想要业务类别中的TOP 2业务,我尝试添加HAVING COUNT(*) <= 2,但它不起作用...

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);

1 个答案:

答案 0 :(得分: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;