1
使用Oracle 11g。Oracle SQL Group整數範圍
我有一張表,其中包含學生ID和標記的列表。另一個查找表包含每個等級的邊界值。
STUDENT_MARKS:
STUDENT_ID STUDENT_MARK
1 20
2 60
3 90
4 20
GRADE_LOOKUP:
GRADE_ID GRADE LOWER_MARK UPPER_MARK
1 A 80 100
2 B 50 79
3 C 0 49
我想寫一個查詢來返回每個年級邊界的學生數。例如:
STUDENT_GRADE STUDENT_COUNT
A 1
B 1
C 2
我已經寫了下面的代碼,它返回正確的結果,但是真實的數據包含大約40個'等級'邊界。因此下面的代碼變得相當笨拙。我理想的是尋找一種方法,使用內置的方法產生相同的結果,沿着GROUP的界限。
SELECT 'A' AS STUDENT_GRADE,
COUNT(STUDENT_ID) AS STUDENT_COUNT
FROM STUDENT_MARKS
WHERE STUDENT_MARK >= (SELECT LOWER_MARK FROM GRADE_LOOKUP WHERE GRADE_ID
= 1)
UNION ALL
SELECT 'B' AS STUDENT_GRADE,
COUNT(STUDENT_ID) AS STUDENT_COUNT
FROM STUDENT_MARKS
WHERE STUDENT_MARK >= (SELECT LOWER_MARK FROM GRADE_LOOKUP WHERE GRADE_ID
= 2)
AND STUDENT_MARK <= (SELECT UPPER_MARK FROM GRADE_LOOKUP WHERE GRADE_ID
=2)
UNION ALL
SELECT 'C' AS STUDENT_GRADE,
COUNT(STUDENT_ID) AS STUDENT_COUNT
FROM STUDENT_MARKS
WHERE STUDENT_MARK <= (SELECT UPPER_MARK FROM GRADE_LOOKUP WHERE GRADE_ID
= 3);
設置代碼包含在下面。謝謝你的幫助。
CREATE TABLE STUDENT_MARKS
(
STUDENT_ID INTEGER PRIMARY KEY,
STUDENT_MARK INTEGER
);
INSERT ALL
INTO STUDENT_MARKS (STUDENT_ID, STUDENT_MARK) VALUES (1, 20)
INTO STUDENT_MARKS (STUDENT_ID, STUDENT_MARK) VALUES (2, 60)
INTO STUDENT_MARKS (STUDENT_ID, STUDENT_MARK) VALUES (3, 90)
INTO STUDENT_MARKS (STUDENT_ID, STUDENT_MARK) VALUES (4, 20)
SELECT * FROM DUAL;
CREATE TABLE GRADE_LOOKUP
(
GRADE_ID INTEGER PRIMARY KEY,
GRADE VARCHAR2(10),
LOWER_MARK INTEGER,
UPPER_MARK INTEGER
);
INSERT ALL
INTO GRADE_LOOKUP (GRADE_ID, GRADE, LOWER_MARK, UPPER_MARK) VALUES
(1, 'A', 80, 100)
INTO GRADE_LOOKUP (GRADE_ID, GRADE, LOWER_MARK, UPPER_MARK) VALUES
(2, 'B', 50, 79)
INTO GRADE_LOOKUP (GRADE_ID, GRADE, LOWER_MARK, UPPER_MARK) VALUES
(3, 'C', 0, 49)
SELECT * FROM DUAL;