在sql server中计算平均评分

332nm8kg  于 2022-11-28  发布在  SQL Server
关注(0)|答案(3)|浏览(269)

这是我table:

我想获取包含ServiceDescription“Plaster”或Skills“Plaster”或在类别“Plaster”中的那些供应商的记录,并且还想计算这些供应商的平均值。
注:如果没有对任何供应商进行审查,那么也应该有记录。
这是我的疑问:

select * from UserDetails u
  ,VendorInCategory v
  ,CategoryMaster c
  ,Review rv
where v.CategoryId=c.Id 
and u.Id=r.UserId 
and u.Id=rv.VendorId  
and v.VendorId=u.Id 
and ((u.ServiceDescription like '%Plaster%' ) 
or (u.Skills like '%Plaster%') 
or (c.Name like '%Plaster%'))

这里的问题在上面的查询是我没有得到的供应商的审查是不存在的。
但我也希望供应商不包含审查,但符合我的标准。

用户详细信息:

id     Servicedescription         Skills
1        Plaster                  plaster

2        construction             construvtion

3        plaster                  plaster

4        null                     null(not vendor)

5        null                     null(not vendor)

检阅

id     CustomerId     Vendorid    rating

1       4                1          3

2       5                1          3

预期输出:

VendorId     ServiceDescription     Skills       averagerating

1              plaster              plaster           3

3              plaster              plaster           0

注:最终输出应按平均评分降序排列

dgsult0t

dgsult0t1#

来试试这个

样本数据

create table UserDetails(
    Id int,
    ServiceDescription varchar(20),
    Skills varchar(20)
)
create table Review(
    Id int,
    CustomerId int,
    VendorId int,
    Rating int
)

insert into UserDetails values(1, 'Plaster', 'plaster'),(2, 'construction', 'construction'),(3, 'plaster', 'plaster');
insert into Review values(1, 4, 1, 3),(2, 5, 1, 3);

溶液

select
    u.Id as VendorId,
    u.ServiceDescription,
    u.Skills,
    isnull(sum(r.rating)/count(r.rating), 0) as AverageRating
from UserDetails u
left join Review r
    on r.VendorId = u.id
where
    u.ServiceDescription like '%plaster%'
    or u.Skills like '%plaster%'
group by 
    u.Id,
    u.ServiceDescription,
    u.Skills
order by AverageRating desc
bq9c1y66

bq9c1y662#

许多用户使用AVERAGE函数来计算一系列数据的平均值。但是,当您有汇总数据而不是单个答复,并且需要计算平均值时,该怎么办呢?(例如,在5分评级尺度(如产品评级)上选择每个评级的人数计数。)

如何计算加权平均值

假设您想要取得每项产品的平均整体评分:

  • (9)9人为等级评定1人。
  • 对于等级2,(13)13人。
  • 为三级,(1)一人。

使用AVERAGE函数将得到平均值7.7。当然,这没有任何意义。我们应该期望平均值在标度范围(1到5)内。
为了正确计算每个问题的平均总体回答,我们需要:
1.将选择每个评分的人数乘以相应的评分值(1 - 5)
1.将这些计算结果加在一起。
1.将结果除以问题的回答总数。

样本数据:

create table #tableRatings(
    Id int,
    Rating numeric(18,6)
)

insert into #tableRatings values(1, 4.3),(2,3.3),(3,4.8);

解决方案:

SELECT
    SUM(
    case 
        WHEN FLOOR(rating) = 1 THEN rating
        WHEN FLOOR(rating) = 2 THEN rating *2
        WHEN FLOOR(rating) = 3 THEN rating *3
        WHEN FLOOR(rating) = 4 THEN rating *4
        WHEN FLOOR(rating) = 5 THEN rating *5        
    end    
    ) / SUM(rating)
    FROM #tableRatings

结果:

3.733870
ssgvzors

ssgvzors3#

使用聚合函数AVG()
试试这个:

SELECT u.id, u.ServiceDescription, u.Skills, u.fullname, u.email, AVG(ISNULL(rv.rating, 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 Review rv ON u.Id=rv.VendorId  
WHERE (u.ServiceDescription LIKE '%Plaster%' OR u.Skills LIKE '%Plaster%' OR 
       c.Name LIKE '%Plaster%')
GROUP BY u.id, u.ServiceDescription, u.Skills, u.fullname, u.email
ORDER BY averagerating DESC;

编辑

实现此功能的其他解决方案:

SELECT u.id, u.ServiceDescription, u.Skills, u.fullname, u.email, 
       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, AVG(rv.rating) averagerating FROM Review rv GROUP BY rv.VendorId) rv ON u.Id=rv.VendorId  
WHERE (u.ServiceDescription LIKE '%Plaster%' OR u.Skills LIKE '%Plaster%' OR 
       c.Name LIKE '%Plaster%')
ORDER BY ISNULL(rv.averagerating, 0) DESC;

相关问题