2016-05-17 5 views
-1

単一のMySQLクエリでは、次の出力に基づいてテストで達成されたパーセンテージ/スコアをどのように練習できますか。指定された回答はtest_answer_idであり、正解はcorrect_answer_idです。以下の例で は、3はその70%ここで単一クエリでMysqlの2つのフィールドに正しく/間違っている割合を計算します

 
id  doctor_id test_id test_question_id test_answer_id correct_test_answer_id 
168836 862   123  2706    3353   3353 
168837 862   123  2707    3354   3354 
168838 862   123  2708    3357   3357 
168839 862   123  2709    3358   3359 
168840 862   123  2710    3360   3360 
168841 862   123  2711    3363   3363 
168842 862   123  2712    3365   3365 
168843 862   123  2713    3367   3366 
168844 862   123  2714    3369   3369 
168845 862   123  2715    3370   3371 
+0

使用しているテーブルの構造は何ですか?あなたが試した質問は何ですか?エラーを投げていますか? – Alfabravo

答えて

1

クエリがあるを返す必要が間違ってスコア化しました。あなたは、質問の総数に対する正解の数を分けるだけです:

select doctor_id, 
     test_id, 
     sum(case when test_answer_id = correct_test_answer_id 
      then 1 else 0 end) * 100.0/count(*) 
from table 
group by doctor_id, test_id 
関連する問題