我的sql查询找到#status

时间:2013-05-22 09:06:38

标签: mysql sql

我有一个表格,其中包含列名称课程和状态,如下所示

Course    Status
---------------------------
Math      Complete
Math      Complete
Math      Complete
Science   Incomplete
Science   Complete
Science   Complete

我想要一个提供以下结果的MySQL查询

Course    % Status_complete
--------------------------
Math        100%
Science     33.3%  

2 个答案:

答案 0 :(得分:1)

SELECT  Course,
        SUM(Status = 'Complete') / COUNT(*) * 1.0 AS STATUS_COMPLETE
FROM    TableName
GROUP   BY Course

答案 1 :(得分:0)

使用: -

SELECT  Course,
        SUM(Status = 'Complete') / COUNT(*) * 100.0 as PercentageComplete,
        SUM(Status = 'Incomplete') / COUNT(*) * 100.0 as PercentageIncomplete
FROM    CourseTable
GROUP   BY Course

输出: -

COURSE    PERCENTAGECOMPLETE    PERCENTAGEINCOMPLETE
Math             100                         0
Science        66.66667                  33.33333