2013-05-22 131 views
0

我有包含列名課程和狀態像下面我的SQL查詢來查找#狀態

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

我想MySQL查詢表誰給下面的結果

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

回答

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