-1

我有以下记录和价值观:

create table student (
  LastName      varchar(40),
  FirstName     varchar(40),
  SID           number(5),
  SSN           number(9),
  Career        varchar(4),
  Program       varchar(10),
  City          varchar(40),
  Started       number(4),

  primary key (SID),
  unique(SSN)
);

create table enrolled (
  StudentID     number(5),
  CourseID      number(4),
  Quarter       varchar(6),
  Year          number(4),

  primary key (StudentID, CourseID),
  foreign key (StudentID) references student(SID),
  foreign key (CourseID) references course(CID)
);

insert into student
    values ( 'Brennigan', 'Marcus', 90421, 987654321, 'UGRD', 'COMP-GPH', 'Evanston', 2001 );
insert into student
    values ( 'Patel', 'Deepa', 14662, null, 'GRD', 'COMP-SCI', 'Evanston', 2003 );
insert into student
    values ( 'Snowdon', 'Jonathan', 08871, 123123123, 'GRD', 'INFO-SYS', 'Springfield', 2005 );
insert into student
    values ( 'Starck', 'Jason', 19992, 789789789, 'UGRD', 'INFO-SYS', 'Springfield', 2003 );
insert into student
    values ( 'Johnson', 'Peter', 32105, 123456789, 'UGRD', 'COMP-SCI', 'Chicago', 2004 );
insert into student
    values ( 'Winter', 'Abigail', 11035, 111111111, 'GRD', 'PHD', 'Chicago', 2003 );
insert into student
    values ( 'Patel', 'Prakash', 75234, null, 'UGRD', 'COMP-SCI', 'Chicago', 2001 );
insert into student
    values ( 'Snowdon', 'Jennifer', 93321, 321321321, 'GRD', 'COMP-SCI', 'Springfield', 2004 );

insert into enrolled
    values (11035, 1020, 'Fall', 2005);
insert into enrolled
    values (11035, 1092, 'Fall', 2005);
insert into enrolled
    values (11035, 8772, 'Spring', 2006);
insert into enrolled
    values (75234, 3201, 'Winter', 2006);
insert into enrolled
    values (08871, 1092, 'Fall', 2005);
insert into enrolled
    values (90421, 8772, 'Spring', 2006);
insert into enrolled
    values (90421, 2987, 'Spring', 2006);

我的目标是:列出(成对)一起上同一门课的学生。

预期输出:

StudentID   COURSEID
11035   1092
8871    1092
11035   8772
90421   8772

我怎样才能做到这一点?

谢谢,

4

1 回答 1

1

请尝试查询:

select * from(
  select a.FirstName, a.LastName, b.StudentID, b.COURSEID, count(*) over (partition by b.COURSEID) CNum
  from STUDENT a inner join enrolled b on a.SID=b.StudentID
  ) x where CNum=2
于 2013-04-25T07:22:24.710 回答