2016-09-22 6 views
1

各テストのカテゴリ(懸念事項)の割合を取得しようとしていますが、2つのクエリを組み合わせる際に問題があります。私が問題を抱えているところは、分けて正しい質問を得ることです。私は参考としてHow to calculate percentage with a SQL statementを使用しました。私は、動的なクエリで各カテゴリ(関心事)の何パーセントが尋ねられているのかを調べることを検討しています。SQLでクエリと%を組み合わせる

表A

SELECT exams_id as exams, Count(*) AS TotalQuestions 
FROM exams_questions AS eq 
JOIN concerns as con ON eq.concerns_id = con.concerns_id 
GROUP BY exams_id 
ORDER BY exams_id 

+--------------+--------------+ 
| Exams  |TotalQuestions|   
+--------------+--------------+ 
| 1   | 200   | 
| 2   | 100   | 
| 3   | 400   | 
| 4   | 150   | 
+--------------+--------------+ 

表B

select exams_id as exam, count(con.concerns_id) as numberOfConcern, con.concerns_description, sum(con.concerns_id) as countTotal 
from exams_questions 
join concerns as con on exams_questions.concerns_id = con.concerns_id 
where exams_id is not null 
group by exams_id, con.concerns_id, con.concerns_description 
order by exams_id asc, con.concerns_id 



+----------------+----------------+------------------+ 
| Exams   |ConcernID  | NumberofConcern | 
+----------------+----------------+------------------+ 
| 1   | 1    | 25    | 
| 1   | 5    | 37    | 
| 1   | 33    | 24    | 
| 1   | 43    | 35    | 
| 1   | 44    | 7    | 
| 1   | 45    | 22    | 
| 1   | 46    | 27    | 
| 1   | 47    | 33    | 
| 2   | 1    | 20    | 
| 2   | 4    | 25    | 
| 2   | 22    | 35    | 
| 2   | 24    | 20    | 
+----------------+----------------+------------------+ 

表を結合

SELECT e.exams_description, eq.exams_id as exams, con.concerns_id as ConcernID, Count(*) as numberofQuestions, Cast(Count(*)* 100.0/Sum(Count(*)) OVER() AS DECIMAL(18, 2)) as ExamPercent 
FROM exams_questions as eq 
JOIN concerns AS con on eq.concerns_id = con.concerns_id 
JOIN exams AS e on e.exams_id = eq.exams_id 
GROUP BY eq.exams_id, con.concerns_id, e.exams_description 
ORDER BY eq.exams_id asc, con.concerns_id 

+-------------------+-----------------+-------------------+---------------+ 
| Exam   | ConcernID  | NumberofConcern | ExamPercent | 
+-------------------+-----------------+-------------------+---------------+ 
| 1    |  1   |  25   | .24   | 
| 1    |  5   |  27   | .26   | 
| 1    |  33   |  24   | .23   | 
| 1    |  43   |  35   | .33   | 
| 1    |  44   |  7    | .07   | 
| 1    |  45   |  22   | .21   | 
| 1    |  46   |  27   | .26   | 
| 1    |  47   |  33   | .31   | 
| 2    |  1   |  20   | .2   | 
| 2    |  4   |  25   | .25   | 
| 2    |  22   |  35   | .35   | 
| 2    |  24   |  20   | .2   | 
+-------------------+-----------------+-------------------+---------------+ 

質問の数が静的であるしかし、試験2のように100件の質問がある場合、これは素晴らしい作品試験で変更してTotalQuestionsを組み込む必要があります。

Cast(count(*)* 100.0/sum(count(*)) over() AS DECIMAL(18, 2)) as ExamPercent   

私が信じているのは、変更が必要な場所です。

ご案内ありがとうございました

+0

私は依頼しようとしていることを解読しようとしています。試験1に200問の質問がある場合、データベースには8試験1のレコードがあります。あなたのパーセンテージが200のパーセンテージを調べようとしていますか? –

答えて

0

あなたは探していますか?

Select EQ.exams_id as exam, count(con.concerns_id) as numberOfConcern, con.concerns_description, sum(con.concerns_id) as countTotal, tot.TotalQuestions, 
     ROUND(CAST((count(con.concerns_id) * 100.0/tot.TotalQuestions) AS FLOAT), 2) AS ExamPercent 
from exams_questions EQ 
join concerns as con on exams_questions.concerns_id = con.concerns_id 
Join (
      SELECT exams_id as exams, Count(*) AS TotalQuestions 
      FROM exams_questions AS eq 
      JOIN concerns as con ON eq.concerns_id = con.concerns_id 
      GROUP BY exams_id 
    ) tot ON EQ.exams_id = tot.exams_id 
where EQ.exams_id is not null 
group by EQ.exams_id, con.concerns_id, con.concerns_description 
order by EQ.exams_id asc, con.concerns_id 

最初のクエリは、exam_idごとの合計を計算します。このようにして、2番目のクエリに参加して、同じ行の各試験の合計を得て、この合計で割合を計算しました。

関連する問題