user2405033
user2405033

Reputation: 1

MY sql query to find the # status

I have a table which contain Column name Course and Status like below

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

I want to a MySQL query who give following result

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

Upvotes: 0

Views: 38

Answers (2)

Vivek Sadh
Vivek Sadh

Reputation: 4268

Use this:-

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

Output :-

COURSE    PERCENTAGECOMPLETE    PERCENTAGEINCOMPLETE
Math             100                         0
Science        66.66667                  33.33333

Upvotes: 0

John Woo
John Woo

Reputation: 263843

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

Upvotes: 1

Related Questions