如何从用户中选择最高评价?

时间:2018-10-19 16:05:27

标签: sql sql-server sql-server-2008

我需要为产品选择评论,但由用户唯一 (即用户的一条评论)

使用我的代码,我选择了所有评论,并且看到一个用户留下的评论很少。

SELECT 
    tr.reviewText, tr.reviewDate, tr.reviewRating,
    u.userName AS userName,
    u.userFirstName AS userFirstName, u.userSurname AS userSurname, 
    u.countryId AS countryId
FROM 
    tblReviews tr
INNER JOIN 
    tblOrderProduct op ON op.orderProductId = tr.orderProductId
                       AND op.productOptionId IN (SELECT productOptionId 
                                                  FROM tblProductOption
                                                  WHERE productSubCuId = 111 
                                                    AND productOptionActive = 1)
LEFT JOIN 
    tblOrder o ON o.orderId = op.orderId
LEFT JOIN 
    tblUser u ON u.userRandomId = o.userRandomId
WHERE 
    tr.reviewsStatusId = 2
ORDER BY 
    tr.reviewRating DESC, tr.reviewDate DESC
    OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY

我只能从每个用户那里获得一条评论吗?

也许我需要按userId选择userId->组结果,然后每组选择一个? [我曾尝试这样做,但没有成功:(]

2 个答案:

答案 0 :(得分:0)

您可以使用row_number来为评论编号,然后选择以下任意一项:

;with per_user_one_review
as
(SELECT tr.reviewText, tr.reviewDate, tr.reviewRating,
        u.userName as userName,
        u.userFirstName as userFirstName, u.userSurname as userSurname, 
        u.countryId as countryId, row_number() over (partition by u.userRandomId order by tr.reviewDate desc) rn
      FROM tblReviews tr
      INNER JOIN tblOrderProduct op 
        ON op.orderProductId = tr.orderProductId
      AND op.productOptionId IN (
        SELECT productOptionId FROM tblProductOption
        WHERE productSubCuId = 111 AND productOptionActive = 1
      )
      LEFT JOIN tblOrder o ON o.orderId = op.orderId
      LEFT JOIN tblUser u ON u.userRandomId = o.userRandomId
      WHERE tr.reviewsStatusId = 2
      ORDER BY tr.reviewRating DESC, tr.reviewDate DESC
      OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY
      )
select * from per_user_one_review where rn = 1

它将从用户那里选择最新的评论(reviewDate desc)。

答案 1 :(得分:0)

如果您需要最后一次审核,则可以与suquery一起使用,以按orderProductId分组的最长审核日期

(并且建议您使用内部联接代替基于子查询的IN clasue)

    select tr.reviewText
            , tr.reviewDate
            , tr.reviewRating
            , u.userName
            , u.userFirstName 
            , u.userSurname 
            , u.countryId 
    from  tblReviews tr 
    INNER JOIN (
        select max(reviewDate) max_date, orderProductId
        from tblReviews 
        group by orderProductId
    ) t1 on t1.orderProductId = tr.orderProductId and t1.max_date = tr.reviewDate
    INNER JOIN tblOrderProduct op ON op.orderProductId = tr.orderProductId
    INNER JOIN (
        SELECT productOptionId 
        FROM tblProductOption
        WHERE productSubCuId = 111 AND productOptionActive = 1
    ) t2 ON op.productOptionId = t2.productOptionId
    LEFT JOIN tblOrder o ON o.orderId = op.orderId
    LEFT JOIN tblUser u ON u.userRandomId = o.userRandomId
    WHERE tr.reviewsStatusId = 2
    ORDER BY tr.reviewRating DESC, tr.reviewDate DESC
    OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY