答案 0 :(得分:1)
这会创建一个更规范化的表结构。还有很多工作要做: - 在STUDENT中添加一列以显示学生目前是否参加 - 添加包以插入,更新,删除数据
CREATE TABLE STUDENT
(ID NUMBER(9),
STUDENT_NAME VARCHAR2(500),
CONSTRAINT PK_STUDENT PRIMARY KEY(ID));
--------
CREATE TABLE ATTENDANCE_TYPE
(ID NUMBER(9),
AT_DESCRIPTION VARCHAR2(250),
CONSTRAINT PK_ATTENDANCE_TYPE PRIMARY KEY(ID))
------------------------
CREATE TABLE ATTENDANCE(
ID NUMBER(9),
STUDENT_ID NUMBER(9),
ATTENDANCE_DAY DATE,
ATTENDANCE_TYPE_ID NUMBER(9),
CONSTRAINT PK_ATTENDANCE PRIMARY KEY(ID),
CONSTRAINT FK_ATTENDANCE_TYPE
FOREIGN KEY (ATTENDANCE_TYPE_ID) REFERENCES ATTENDANCE_TYPE(ID),
CONSTRAINT FK_STUDENT
FOREIGN KEY (STUDENT_ID) REFERENCES STUDENT(ID));
--this gets the number of students late in Jan, Feb and March 1st this year
SELECT COUNT(*)
FROM student s JOIN attendance att
ON s.ID = att.student_id
WHERE att.attendance_type_id = 2
AND att.attendance_day BETWEEN TO_DATE('01-JAN-2016', 'DD-MON-YYYY') AND TO_DATE('01-MAR-2016', 'DD-MON-YYYY');