这些是我的表
CREATE TABLE student(
student_id NUMBER(4) PRIMARY KEY,
name Varchar2(18) );
CREATE TABLE subject(
subject_id NUMBER(4) PRIMARY KEY,
name Varchar2(18) );
CREATE TABLE marksheet(
id NUMBER(4) ,
student_id NUMBER(4) REFERENCES student(student_id) NOT NULL,
subject_id NUMBER(4) REFERENCES subject(subject_id) NOT NULL,
grade VARCHAR2(1) NOT NULL,
total_marks NUMBER(3) NOT NULL,
marks_obtain NUMBER(3) NOT NULL );
insert into student values(2575, 'Tom');
insert into student values(2576, 'jerry');
INSERT INTO subject VALUES(10,'English');
INSERT INTO subject VALUES(20,'Database');
INSERT INTO subject VALUES(30,'Calculas');
INSERT INTO subject VALUES(40,'OOAD');
INSERT INTO subject VALUES(50,'Management');
INSERT INTO subject VALUES(60,'java');
INSERT INTO marksheet VALUES(1,2575,10,'A', 100, 80);
INSERT INTO marksheet VALUES(1,2575,20,'A', 100, 80);
INSERT INTO marksheet VALUES(1,2575,30,'A', 100, 80);
INSERT INTO marksheet VALUES(1,2575,40,'A', 100, 80);
INSERT INTO marksheet VALUES(1,2575,50,'A', 100, 80);
在标记表中插入数据
我想要的是它应该只显示
学生姓名一次
student_id一次
其余的标记表(等级,total_marks,marks_obtain)
答案 0 :(得分:2)
使用row_number()
并仅为编号为1
的行显示ID和名称:
select case when rn = 1 then student_id end student_id,
case when rn = 1 then name end name,
subject_id, subject, grade, total_marks, marks_obtain
from (
select m.student_id, st.name, m.subject_id, sb.name subject,
m.grade, m.total_marks, m.marks_obtain,
row_number() over (partition by m.student_id order by m.subject_id) rn
from marksheet m
join student st on st.student_id = m.student_id
join subject sb on sb.subject_id = m.subject_id)
输出:
STUDENT_ID NAME SUBJECT_ID SUBJECT GRADE TOTAL_MARKS MARKS_OBTAIN
---------- ------------------ ---------- ------------------ ----- ----------- ------------
2575 Tom 10 English A 100 80
20 Database A 100 80
30 Calculas A 100 80
40 OOAD A 100 80
50 Management A 100 80