3表:如何構建查詢?任何想法?
馬克表:
student_id sa_id marks
1 1 75
1 2 80
1 3 100
2 4 85
2 5 90
2 6 60
當然表:
course_code sat_id name_code
AAA 100 1 midterm1
AAA 100 2 midterm2
AAA 100 3 final
BBB 200 4 midterm1
BBB 200 5 midterm2
BBB 200 6 final
變換表:
sa_id sat_id
1 1
2 2
3 3
4 4
5 5
6 6
select course.course_code, mark.marks
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
where course.name_code = 'midterm1'
在上面的查詢只midterm1結果,我們也可以提取MID2和最終
select mark.student_id,course.course_code, mark.marks, course.name_code
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
order by mark.student_id, course.course_code
結果會給:
student_id course_code marks name_code
1 AAA 100 75 midterm1
1 AAA 100 80 midterm2
1 AAA 100 100 final
2 BBB 200 85 midterm1
2 BBB 200 90 midterm2
2 BBB 200 60 final
因此,如何建立查詢,應該是
student_id course_code midterm1 midterm2 final
1 AAA 100 75 80 100
2 BBB 200 85 90 60
@scaisEdge爲什麼你使用max?使用它很重要嗎? – jaraisyn
這很重要,因爲您必須按'student_id'分組。無論何時你分組,在SELECT中你只能按GROUP BY中的列進行分組,否則你必須使用像MAX()這樣的集合函數。你知道每個組只有一列,但Oracle不知道,如果你沒有使用聚合函數,它會發出抱怨(給出錯誤)。 – mathguy