2013-11-25 90 views
4

我需要生成SQL數據庫的幫助。計算研究區域的百分比

我有兩個表:

  • 學生
  • student_Subjects

學生表:

 
+------------+---------+-----------------+ 
| id  | name |area_of_study_id | 
+------------+---------+-----------------+ 
| 1   | AAA  | 1    | 
| 2   | BBB  | 2    | 
| 3   | CCC  | 1    | 
| 4   | DDD  | 3    | 
| 5   | EEE  | 4    | 
| 6   | FFF  | 1    | 
| 7   | GGG  | 2    | 
| 8   | III  | 1    | 
+------------+---------+-----------------+ 

student_subjects表:

 
+------------+-------------------+------------------+ 
| id  | student_id  | subject_id  | 
| 1   | 1     |  1   | 
| 2   | 2     |  1   | 
| 3   | 1     |  3   | 
| 4   | 1     |  2   | 
| 5   | 3     |  1   | 
| 6   | 6     |  1   | 
| 7   | 7     |  3   | 
| 8   | 4     |  6   | 
+------------+-------------------+------------------+ 

我需要找出基於students area of study一個subject's area of study。從學生表上可以看到,學生可以自由選擇area of study。有時,他們可以選擇與其學習領域無關的科目。因此,在這種情況下,我們需要根據已採納主題的student percentages來計算area of studysubject

的部分輸出:

 
+------------+-------------------+-----------------------------------+ 
|Total_students | subject_id |area_of_study_id | percentage  | 
+------------+-------------------+-----------------+-----------------+ 
| 3   | 1     | 1    | 75    | 
| 1   | 1     | 2    | 25    | 
| 1   | 3     | 1    | 50    | 
| 1   | 3     | 2    | 50    | 
| 1   | 2     | 1    | 100    | 
| 1   | 6     | 3    | 100    | 
+------------+-------------------+-----------------+-----------------+ 

從上面的部分輸出中,我們可以看到,3名學生選擇的受試者(即subject_id = 1)具有研究1的面積和一個學生是來自不同區研究(即2)。所以,我們可以說subject_id =1是從area of study = 1

如果百分比50%爲一個主題。我們可以選擇兩個之間的任何學習領域,不管是哪一個。

預期輸出:

 
+------------+-------------------+-----------------------------------+ 
|Total_students | subject_id |area_of_study_id | percentage  | 
+------------+-------------------+-----------------+-----------------+ 
| 3   | 1     | 1    | 75    | 
| 1   | 3     | 1    | 50    | 
| 1   | 2     | 1    | 100    | 
| 1   | 6     | 3    | 100    | 
+------------+-------------------+-----------------+-----------------+ 

在先進的感謝。

+0

能詳細百分比如何爲局部的,最終的結果計算解釋一下嗎? – peterm

+0

只計算一次的百分比。在最後我只是過濾部分。部分地,你可以看到4個學生選擇subject_id = 1。但是,一個來自不同的學習領域。所以百分比計算:100 * 3/4 = 75和單身學生; 100 x 1/4 = 25。 – Anam

+0

@Anam:我認爲如果您分別將主題名稱和研究區域名稱,而不是研究ID的主題ID和區域,將會更有幫助。由於數字的相似性,這非常令人困惑。 – Edper

回答

1

嘗試這種方式

SELECT subject_id, area_of_study_id, total_students, percentage 
    FROM 
(
    SELECT p.subject_id, p.area_of_study_id, p.total_students, 
     p.total_students/t.total_students * 100 percentage 
    FROM 
    (
    SELECT COUNT(*) total_students, ss.subject_id, s.area_of_study_id 
     FROM students s JOIN student_subjects ss 
     ON s.id = ss.student_id 
    GROUP BY ss.subject_id, s.area_of_study_id 
) p JOIN 
    (
    SELECT ss.subject_id, COUNT(*) total_students 
     FROM students s JOIN student_subjects ss 
     ON s.id = ss.student_id 
    GROUP BY ss.subject_id 
) t ON p.subject_id = t.subject_id 
    ORDER BY percentage DESC 
) q 
    GROUP BY subject_id; 

輸出:

 
| SUBJECT_ID | AREA_OF_STUDY_ID | TOTAL_STUDENTS | PERCENTAGE | 
|------------|------------------|----------------|------------| 
|   1 |    1 |    3 |   75 | 
|   2 |    1 |    1 |  100 | 
|   3 |    1 |    1 |   50 | 
|   6 |    3 |    1 |  100 | 

這裏是SQLFiddle演示

+0

@Anam有幫助嗎? – peterm

+0

感謝您的幫助。抱歉我當時在忙。這個查詢是龐大而複雜的。我很高興你幫助了我,節省了大量的時間。再次感謝。 – Anam