请参阅我已经问过的这个问题:
calculate average rating in sql server
一切都很完美。我也得到了我的愿望输出,但我只是想要得到评论的客户名称。
这是我从上面链接中获取的查询:
SELECT u.id,u.fullname,u.email,u.ServiceDescription, u.Skills,c.Name,rv.Reviews,
ISNULL(rv.averagerating, 0) averagerating
FROM UserDetails u
INNER JOIN VendorInCategory v ON v.VendorId=u.Id
INNER JOIN CategoryMaster c ON v.CategoryId=c.Id
LEFT JOIN (SELECT rv.VendorId,rv.Reviews, AVG(rv.RatingValue) averagerating FROM Review rv GROUP BY rv.VendorId,rv.Reviews) rv ON u.Id=rv.VendorId
WHERE ((u.ServiceDescription LIKE '%SearchKeyword%') OR (u.Skills LIKE '%@SearchKeyword%') OR
(c.Name LIKE '%@SearchKeyword%'))
ORDER BY ISNULL(rv.averagerating, 0) DESC;
现在在该查询中,可以获取已提供评论的客户名称
的UserDetails:
id Name Servicedescription Skills
1 john Plaster plaster
2 abc construction construction
3 xyz plaster plaster
4 pqr null null(not vendor)
5 lmn null null(not vendor)
查看
id CustomerId Vendorid rating reviews
1 4 1 3 fdd
2 5 1 3 dfg
预期输出:
VendorId ServiceDescription Skills rating customername reviews
1 plaster plaster 3 pqr good
3 plaster plaster 3 lmn bad
我不想计算平均评分。
任何人都可以帮我做这个????
答案 0 :(得分:1)
只需将用户表和评论表之间的Left Join
设为Inner Join
,然后从用户表中选择名称列
尝试使用以下查询
示例数据
create table UserDetails(
Id int,
Name Varchar(250),
ServiceDescription varchar(20),
Skills varchar(20)
)
create table Review(
Id int,
CustomerId int,
VendorId int,
Rating int,
review Varchar(25)
)
insert into UserDetails values(1, 'John','Plaster', 'plaster'),(2,'Paul', 'construction', 'construction'),(3,'AAA', 'plaster', 'plaster'),(4,'PQR',null,null),
(5,'lmn',null,null)
insert into Review values(1, 4, 1, 3,'Good'),(2, 5, 1, 0,'Bad');
<强>查询强>
select
VendorId=r.CustomerId,
vu.ServiceDescription,
vu.Skills,
AverageRating=r.rating,
CustomerName=ru.Name,
reviews=r.review
FROM Review r
JOIN UserDetails vu ON r.Vendorid = vu.id
JOIN UserDetails ru ON r.CustomerId = ru.id
where
ru.ServiceDescription is null
or ru.Skills is null
group by
r.CustomerId,
ru.Name,
vu.ServiceDescription,
vu.Skills,r.rating,r.review
order by AverageRating desc
答案 1 :(得分:0)
你不能按照你想要的方式做到这一点,因为平均值是一个平均值:如果十个不同的人审查了那个供应商怎么办?它应该显示谁的名字?
但是,每次审核都可以有一个单独的行。
如果这就是你想要的,试试这个。
编辑:已更新,以包含Review.reviews。
SELECT vu.id AS VendorId, vu.ServiceDescription, vu.Skills,
r.RatingValue AS rating, ru.name AS customername, r.reviews
FROM Review r
JOIN UserDetails vu ON r.Vendorid = vu.id
JOIN UserDetails ru ON r.CustomerId = ru.id
答案 2 :(得分:0)
根据我得到的答案,我准备了这个查询,预计输出即将到来:
SELECT u.id,u.fullname,u.email,u.ServiceDescription,
u.Skills,c.Name,r.Reviews,r.RatingValue,ru.Id,
ru.Fullname FROM UserDetails u
INNER JOIN VendorInCategory v ON v.VendorId=u.Id
INNER JOIN CategoryMaster c ON v.CategoryId=c.Id
left join Review r
on r.VendorId = u.id
left JOIN UserDetails ru ON r.CustomerId = ru.id
WHERE ((u.ServiceDescription LIKE '%Plaster%') OR (u.Skills LIKE '%Plaster%') OR
(c.Name LIKE '%Plaster%'))
ORDER BY ISNULL(r.RatingValue, 0) DESC;
现在您可以从下面看到链接我的数据库设计:
calculate average rating in sql server
在这里你可以看到石膏是建筑的子类别(Id:40)
所以如果找到我在categorymaster中搜索的任何单词,我想获取parentcategory。
如果在子类别中找到单词,则获取其父类别名称,否则仅获取父类别名称。
任何人都可以编辑我的查询来实现这个功能???