calculate average rating in sql server

前端 未结 3 922
耶瑟儿~
耶瑟儿~ 2021-01-14 17:52

this is my table:

\"enter

I want to fetch records of Those Vendor which contai

3条回答
  •  萌比男神i
    2021-01-14 18:54

    Here, try this:

    SAMPLE DATA

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

    SOLUTION

    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
    

提交回复
热议问题