简体   繁体   中英

SQL GROUP BY QUERY Not Returning desired output

Here are my tables and insert values:

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);

I have the following query:

SELECT e.studentid
FROM enrolled e 
FULL OUTER JOIN student s ON e.studentid = s.sid 
WHERE ((e.quarter = 'Fall') OR (e.quarter = 'Spring')) 
GROUP BY e.studentid 
HAVING count(e.studentid) = 1;

But this only returns

8871

This should return

8871
90421

The goal of this query is: List students who were enrolled in at least one course in Fall quarter or at least one course in the Spring quarter, but not both. You will have to add a couple of rows to test your query.

Any help would be appreciated it. Thanks.

give this a try,

SELECT  a.SID
FROM    student a
        INNER JOIN enrolled b
            ON a.SID = b.StudentID
WHERE   b.quarter IN ('Fall', 'Spring')
GROUP   BY a.SID
HAVING  COUNT(DISTINCT b.quarter) = 1
SELECT  e.studentid
FROM    enrolled e 
WHERE   e.quarter IN ('Spring', 'Fall')
GROUP BY
        e.studentid 
HAVING  COUNT(DISTINCT quarter) = 1;

90421 is enrolled in two different courses during spring, that's why your original query counted it twice.

You need to count seasons, not enrollments.

学生ID有两条记录-90421,因此计数将返回2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM