GROUPED上的一个字段中的多行结果是Mysql

时间:2012-04-23 21:15:26

标签: mysql group-by

我有这样的查询结果。

    status      count
    -----------------
    OTHER       5
    ATTENDED    74

我想要这样的结果:

    OTHERS        ATTENDED
     5                74

请注意,这已经是分组结果。所以我在想,Group_Concat可能不起作用。

查询:

   SELECT CASE Rstat WHEN 1 THEN 'ATTENDED' 
                            WHEN 2 THEN 'TENTATIVE' 
                            WHEN 3 THEN 'REJECTED' 
                            WHEN 4 THEN 'OUTSTANDING' 
                            WHEN 6 THEN 'ACCEPTED' ELSE 'OTHER' END AS 'status',      
                            COUNT(Rstat) AS 'count' 
       FROM  `eStatus` 
       GROUP BY RSTAT

还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

SELECT SUM(CASE WHEN Rstat = 1 THEN 1 ELSE 0 END) AS ATTENDED,
       SUM(CASE WHEN Rstat = 2 THEN 1 ELSE 0 END) AS TENTATIVE,
       SUM(CASE WHEN Rstat = 3 THEN 1 ELSE 0 END) AS REJECTED,
       SUM(CASE WHEN Rstat = 4 THEN 1 ELSE 0 END) AS OUTSTANDING,
       SUM(CASE WHEN Rstat = 6 THEN 1 ELSE 0 END) AS ACCEPTED,
       SUM(CASE WHEN Rstat NOT IN (1,2,3,4,6) THEN 1 ELSE 0 END) AS OTHER
    FROM eStatus