我需要生成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 study
的subject
。
的部分輸出:
+------------+-------------------+-----------------------------------+ |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 | +------------+-------------------+-----------------+-----------------+
在先進的感謝。
能詳細百分比如何爲局部的,最終的結果計算解釋一下嗎? – peterm
只計算一次的百分比。在最後我只是過濾部分。部分地,你可以看到4個學生選擇subject_id = 1。但是,一個來自不同的學習領域。所以百分比計算:100 * 3/4 = 75和單身學生; 100 x 1/4 = 25。 – Anam
@Anam:我認爲如果您分別將主題名稱和研究區域名稱,而不是研究ID的主題ID和區域,將會更有幫助。由於數字的相似性,這非常令人困惑。 – Edper