分组不返回0值

时间:2012-07-04 07:33:46

标签: asp.net mysql sql

我的表格包含pk_idreviewer_idrating。 有4种评级。

1-very good.
2-good.
3-bad.
4-very bad.

我想计算每位评论者给出的评分。 手段: 如果Akee的id为200,则对不同的代码给出2个非常好的,4个好的,3个坏的和非常差的评级。

我想要结果

  count--- rate

   2---------1

   4---------2

   3---------3

   0---------4

我的查询是

SELECT COUNT(RATE),RATE
  FROM CODE_REVIEW WHERE CODE_REVIEWER_ID= 200
 GROUP BY RATE;

显示结果

  count--- rate

   2---------1

   4---------2

   3---------3

我想显示第4行为4等级为零。 怎么办?

6 个答案:

答案 0 :(得分:1)

如果Rate不是另一个表中的主键,那么您需要定义自己的速率列表,以便MySQL知道rate的排列是什么:

SELECT  Rates.Rate,
        COUNT(Code_Review.Rate) AS CountOfRate
FROM    (   SELECT 1 AS Rate UNION ALL
            SELECT 2 AS Rate UNION ALL
            SELECT 3 AS Rate UNION ALL
            SELECT 4
        ) AS Rates
        LEFT JOIN Code_Review
            ON Code_Review.Rate = Rates.Rate
            AND CODE_REVIEWER_ID = 200
GROUP BY Rates.Rate

答案 1 :(得分:0)

假设您没有单独的表来定义费率。

SElECT * from (
  SELECT distinct(m.rate), countrate from code_review m 
  LEFT JOIN
  (SELECT COUNT(rate) as countrate,rate FROM code_review 
     WHERE code_reviewer_id=200 GROUP BY rate) t 
  ON m.rate=t.rate) a

答案 2 :(得分:0)

如果可以,您应该尝试以列格式获取它,因为它很简单:

SELECT
    SUM(rate = 1) AS 1,
    SUM(rate = 2) AS 2,
    SUM(rate = 3) AS 3,
    SUM(rate = 4) AS 4
FROM
    code_review
WHERE
    code_reviewer_id = 200

但如果你真的需要行格式,你可以这样做:

SELECT
    a.rate,
    COUNT(b.rate) AS cnt
FROM
    (
        SELECT 1 AS rate UNION ALL
        SELECT 2 AS rate UNION ALL
        SELECT 3 AS rate UNION ALL
        SELECT 4 AS rate
    ) a
LEFT JOIN
    code_review b ON a.rate = b.rate AND code_reviewer_id = 200
GROUP BY
    a.rate

答案 3 :(得分:0)

你可以这样做

SELECT
            rates.RATE
            , SUM(COUNT) COUNT
   FROM
           (
           SELECT 1 RATE, 0 COUNT UNION ALL
           SELECT 2 RATE, 0 COUNT UNION ALL
           SELECT 3 RATE, 0 COUNT UNION ALL
           SELECT 4 RATE, 0 COUNT
           ) Rates
       LEFT JOIN
           (
               SELECT
                         RATE
                       , COUNT(RATE) COUNT 
                   FROM
                       CODE_REVIEW 
                   WHERE
                       CODE_REVIEWER_ID= 200
                   GROUP BY RATE
           ) Ratings200
           ON Ratings200.RATE = Rates.RATE

答案 4 :(得分:0)

尝试此查询:

SELECT coalesce(c.cnt, 0), r.rate
  FROM (SELECT 1 AS rate UNION ALL SELECT 2
        UNION ALL SELECT 3 UNION ALL SELECT 4) AS r
  LEFT JOIN (SELECT COUNT(RATE),RATE
          FROM CODE_REVIEW WHERE CODE_REVIEWER_ID= 200
         GROUP BY RATE) AS c
    ON r.rate = c.rate;
  1. 第一个子查询创建可能的费率列表。如果您有一个定义所有费率的表格,您可以避免它;
  2. 第二个子查询是你的;
  3. LEFT JOIN保证所有费率都会显示;
  4. coalesce()需要将NULL转换为0。

答案 5 :(得分:0)

SELECT 
    Rate,
    totCount
FROM 
(
    Select
        Rate,
        count(Rate) as totCount
    from
        Code_Review 
    where 
        CODE_REVIEWER_ID  = 200  
    group by 
        Rate
    union
    select  4, 0  
    union
    select  3, 0  
    union
    select  2, 0  
    union
    select  1, 0 
) AS T 
group by  
    T.Rate