开发者

modifying the output of a SP

In my SQl server Sp.

`SELECT rating as [Rating],count(id) as [RatingCount]
FROM MMBPollResults
where mmb_id = @MMbid
GROUP BY rating 

This SP returns the rating for each user.
i:e rating    ratingcount
`    1      开发者_运维知识库     2
     2           1
     5           4

So this means that

2users have rated the transaction with 1star
1 user has rated the transaction with 2stars
4 users have rated the transaction with 5stars

This is how I need the output

rating    ratingcount
`    1           2
     2           1
     3           0
     4           0
     5           4

Sorry, if this is a silly question Thanks Sun


You need a table with 1 to 5. This could be a number table or some other rating table.

Here I use a simple UNION to make a table with 1 to 5

SELECT 
    List.Rating,
    count(MMB.*) as [RatingCount]
FROM
    (
    SELECT 1 AS Rating
    UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
    ) List
    LEFT JOIN
    MMBPollResults MMB ON List.Rating = MMB.Rating AND MMB.mmb_id = @MMbid
GROUP BY
    List.Rating 
ORDER BY
    List.Rating;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜