2016-09-22 71 views
1

我一直試圖從每個測試中獲得類別(關注點)的百分比,但是我在組合這兩個查詢時遇到了問題。我遇到問題的地方在於劃分並得到正確的查詢。我用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記錄。你想看看什麼百分比8是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的總數。因此,我已將它加入到第二個查詢中,以獲得同一行中每個考試的總計,然後用此總額計算百分比。