我在尝试让这个sql语句工作时遇到问题。这个间隙应该像这样计算。主要GPA的计算方法与GPA相同,但计算中仅包括学生专业的课程。例如,假设CS学生在CS课程中获得A,在数学课程中获得B,学生的主要GPA将为4.0。
列出学生及其主要GPA。结果不需要包括没有参加过专业课程的学生。
到目前为止,我有以下查询,但没有完全返回我想要的内容。
select sum(value*units)/sum(units) as GPA ,f.name from faculty f
inner join sections s on s.instructor_id = f.id
inner join courses c on c.id=s.id
inner join grades g on g.value =c.units
group by f.name
我得到的表是
name gpa
Art 4.00
Bob 4.00
Pat 3.30
Amy 3.30
Kim 3.30
Sue 3.30
Joe 2.70
Lee 2.70
Max 2.70
这几乎是我想要的,但它显示了甚至没有参加任何课程的学生的gpas。
数据库看起来像
create table departments (
id integer primary key,
name varchar(255)
);
insert into departments (id, name) values (10, 'Computer Science');
insert into departments (id, name) values (20, 'Math');
insert into departments (id, name) values (30, 'Drama');
create table students (
id integer primary key,
name varchar(255),
graduation_date date,
major_id integer references departments(id)
);
insert into students (id, name, graduation_date, major_id) values
(1, 'Joe', null, 10);
insert into students (id, name, graduation_date, major_id) values
(2, 'Amy', '2009-04-22', 20);
insert into students (id, name, graduation_date, major_id) values
(3, 'Max', null, 10);
insert into students (id, name, graduation_date, major_id) values
(4, 'Sue', '2009-01-10', 20);
insert into students (id, name, graduation_date, major_id) values
(5, 'Bob', '2009-03-05', 30);
insert into students (id, name, graduation_date, major_id) values
(6, 'Kim', null, 20);
insert into students (id, name, graduation_date, major_id) values
(7, 'Art', null, 30);
insert into students (id, name, graduation_date, major_id) values
(8, 'Pat', '2005-07-11', 20);
insert into students (id, name, graduation_date, major_id) values
(9, 'Lee', null, 10);
create table grades (
id integer primary key,
letter varchar(255) not null unique,
value real
);
insert into grades (id, letter, value) values (1, 'A', 4.0);
insert into grades (id, letter, value) values (2, 'A-', 3.7);
insert into grades (id, letter, value) values (3, 'B+', 3.3);
insert into grades (id, letter, value) values (4, 'B', 3.0);
insert into grades (id, letter, value) values (5, 'B-', 2.7);
insert into grades (id, letter, value) values (6, 'C+', 2.3);
insert into grades (id, letter, value) values (7, 'C', 2.0);
insert into grades (id, letter, value) values (8, 'C-', 1.7);
insert into grades (id, letter, value) values (9, 'D+', 1.3);
insert into grades (id, letter, value) values (10, 'D', 1.0);
insert into grades (id, letter, value) values (11, 'D-', 0.7);
insert into grades (id, letter, value) values (12, 'F', 0.0);
insert into grades (id, letter) values (13, 'CR');
insert into grades (id, letter) values (14, 'NC');
create table courses (
id integer primary key,
title varchar(255),
units integer,
department_id integer references departments(id)
);
insert into courses (id, title, units, department_id) values
(12, 'Databases', 4, 10);
insert into courses (id, title, units, department_id) values
(22, 'Compilers', 4, 10);
insert into courses (id, title, units, department_id) values
(32, 'Calculus 1', 4, 20);
insert into courses (id, title, units, department_id) values
(42, 'Algebra', 4, 20);
insert into courses (id, title, units, department_id) values
(52, 'Acting', 4, 30);
insert into courses (id, title, units, department_id) values
(62, 'Elocution', 2, 30);
insert into courses (id, title, units, department_id) values
(72, 'Calculus 2', 4, 20);
insert into courses (id, title, units, department_id) values
(82, 'Topology', 4, 20);
create table sections (
id integer primary key,
course_id integer not null references courses(id),
instructor_id integer references faculty(id),
year integer
);
insert into sections (id, course_id, instructor_id, year) values
(12, 12, 6, 2007);
insert into sections (id, course_id, instructor_id, year) values
(13, 12, 1, 2008);
insert into sections (id, course_id, instructor_id, year) values
(14, 22, 1, 2008);
insert into sections (id, course_id, instructor_id, year) values
(23, 12, 6, 2009);
insert into sections (id, course_id, instructor_id, year) values
(24, 22, 1, 2009);
insert into sections (id, course_id, instructor_id, year) values
(32, 32, 2, 2008);
insert into sections (id, course_id, instructor_id, year) values
(33, 32, 2, 2009);
insert into sections (id, course_id, instructor_id, year) values
(34, 82, 2, 2009);
insert into sections (id, course_id, instructor_id, year) values
(43, 32, 3, 2008);
insert into sections (id, course_id, instructor_id, year) values
(51, 62, 4, 2008);
insert into sections (id, course_id, instructor_id, year) values
(52, 52, 4, 2008);
insert into sections (id, course_id, instructor_id, year) values
(53, 62, 4, 2009);
insert into sections (id, course_id, instructor_id, year) values
(54, 52, 4, 2009);
create table enrollment (
id integer primary key,
student_id integer not null references students(id),
section_id integer not null references sections(id),
grade_id integer references grades(id)
);
insert into enrollment (id, student_id, section_id, grade_id) values
(14, 1, 12, 8);
insert into enrollment (id, student_id, section_id, grade_id) values
(15, 1, 13, 3);
insert into enrollment (id, student_id, section_id, grade_id) values
(16, 1, 14, 5);
insert into enrollment (id, student_id, section_id, grade_id) values
(17, 1, 32, 1);
insert into enrollment (id, student_id, section_id, grade_id) values
(18, 1, 34, 2);
insert into enrollment (id, student_id, section_id, grade_id) values
(19, 1, 53, 13);
insert into enrollment (id, student_id, section_id, grade_id) values
(24, 3, 12, 2);
insert into enrollment (id, student_id, section_id, grade_id) values
(25, 3, 14, 5);
insert into enrollment (id, student_id, section_id, grade_id) values
(26, 3, 32, 1);
insert into enrollment (id, student_id, section_id, grade_id) values
(27, 3, 34, 2);
insert into enrollment (id, student_id, section_id, grade_id) values
(28, 3, 54, 7);
insert into enrollment (id, student_id, section_id, grade_id) values
(34, 2, 43, 3);
insert into enrollment (id, student_id, section_id, grade_id) values
(44, 4, 33, 4);
insert into enrollment (id, student_id, section_id, grade_id) values
(54, 4, 53, 1);
insert into enrollment (id, student_id, section_id, grade_id) values
(64, 6, 53, 1)
答案 0 :(得分:0)
我尝试过您的样本,但无法获得与您相同的结果。
我试过了:
select st.name, sum(value*units)/sum(units) as GPA
from sections s
inner join enrollment e on e.section_id = s.id
inner join students st on st.id = e.student_id
inner join courses c on c.id=s.id
inner join grades g on g.value =c.units
group by st.name
我在那里过滤学生和报名表,这意味着如果学生没有课程,他将不会参加结果
适合你吗?