我正在尝试通过查看 CategoryBusinessMapping 和 Review Rating 表来检索 CategoryID 和 CategoryName。我正在尝试检索以下类别表的数据:
Category ParentCategoryID CategoryName
1 null Education
2 1 School
3 null Health
4 3 Doctors
5 1 Colleges
我有Business表,其中包含 BusinessID 和 BusinessName 以及 BusinessDescription,如下所示:
BusinessID BusinessName BusinessDescription
YP00001 XYZ ABCD
YP00002 ABC XYZA
我有这样的CategoryBusinessMapping表:
MappingID CategoryID BusinessID
1 1 YP00001
2 2 YP00001
3 5 YP00001
4 3 YP00002
5 4 YP00002
我有这个映射表来映射不同的类别与业务。我也有这样的评级表:
RatingID BusinessID
1 YP00001
2 YP00001
3 YP00001
4 YP00002
5 YP00002
在此表中,我假设具有相同 BusinessID 的记录属于最流行的业务。这意味着,在上面 ID = YP00001 的业务 ABCD 中,Rating 表中有四条记录。因此,它属于最受欢迎的业务。同样,YP00002 位于 YP00001 之后。通过按降序查看最受欢迎的业务,我想检索 CategoryName 和 CategoryID。我已尝试仅从评级表中检索:
select Distinct ReviewRating.BusinessID
,Count(*)as Rating
from YP.utblYPReviewRatingDtls as ReviewRating
group by ReviewRating.BusinessID
order by Rating desc
我试过这个:
SELECT distinct c.CategoryName, b.BusinessID
FROM Category c
INNER JOIN categoryBusinessMapping cbm
ON (c.CategoryID=cbm.CategoryID)
INNER JOIN Business b
ON (cbm.BusinessID=b.BusinessID)
LEFT JOIN Rating r
ON (cbm.BusinessID=r.BusinessID)
where c.ParentCategoryID is null
但我得到的结果是多余的。我还从查询中删除了 BusinessID,我得到了结果,但结果不正确。如何消除冗余并获得正确的输出?