Reputation: 1
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
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
Reputation: 263843
SELECT Course,
SUM(Status = 'Complete') / COUNT(*) * 1.0 AS STATUS_COMPLETE
FROM TableName
GROUP BY Course
Upvotes: 1