Hi I have a schema that looks like this
I was trying to make these 3 queries
Find the names of the top 4 instructors who have taught the most number of distinct courses. Display also the total number of courses taught.
Output columns: InstructorName, NumberOfCoursesTaught
Sort by: NumberOfCoursesTaught in descending order
Find the top 3 semesters in which the most number of courses were offered. (Treat Spring of 2009 and Spring of 2010 as two different semesters.
Output columns: Semester, Year, NumberOfCourses
Sort by: NumberOfCourses in descending order
Find the top 2 students who have taken the most number of courses.
Output columns: S_ID, StudentName, NumberOfCourses
Sort by: NumberOfCourses in descending order
For query 1 I wrote
Select name AS InstructorName, count(course_id) AS NumberOfCourses
from Teaches where name IN
(SELECT name FROM Instructor where Instructor.i_id = Teaches.i_id)
group by i_id
order by count(course_id) DESC;
For query 2
SELECT semester, year, count(course_id) as
NumberOfCourses from Takes WHERE year='2009'
group by semester, year
order by count(course_id) DESC;
For query 3
SELECT s_id as S_ID, name as StudentName, count(course_id) as NumberOfCourses
FROM Takes where name IN
(SELECT name from Student where Takes.s_id = Student.s_id)
group by s_id
order by count(course_id) DESC;
Query 1 and 3 give the error
ORA-00904: "NAME": invalid identifier
Query 2 is giving an output, but it's wrong. I need help making the 3 queries correct
Test Data is
tables file is
create table classroom (building varchar(15), room_number varchar(7), capacity numeric(4,0), primary key (building, room_number));
create table department (dept_name varchar(20), building varchar(15), budget numeric(12,2) check (budget > 0), primary key (dept_name));
create table course (course_id varchar(8), title varchar(50), dept_name varchar(20), credits numeric(2,0) check (credits > 0),
primary key(course_id));
create table instructor (i_ID varchar(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2) check (salary > 29000), primary key (i_ID));
create table section (course_id varchar(8), sec_id varchar(8), semester varchar(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')), year numeric(4,0) check (year > 1701 and year < 2100), building varchar(15), room_number varchar(7), time_slot_id varchar(4), primary key (course_id, sec_id, semester, year));
create table teaches (i_ID varchar(5), course_id varchar(8), sec_id varchar(8), semester varchar(6), year numeric(4,0), primary key (i_ID, course_id, sec_id, semester, year));
create table student (s_ID varchar(5), name varchar(20) not null, dept_name varchar(20), tot_cred numeric(3,0) check (tot_cred >= 0), primary key (s_ID));
create table takes (s_ID varchar(5), course_id varchar(8), sec_id varchar(8), semester varchar(6), year numeric(4,0), grade varchar(2), primary key (s_ID, course_id, sec_id, semester, year));
create table advisor (s_ID varchar(5), i_ID varchar(5), primary key (s_ID));
create table time_slot (time_slot_id varchar(4), day varchar(1),start_hr numeric(2) check (start_hr >= 0 and start_hr < 24), start_min numeric(2) check (start_min >= 0 and start_min < 60), end_hr numeric(2) check (end_hr >= 0 and end_hr < 24), end_min numeric(2) check(end_min >= 0 and end_min < 60), primary key (time_slot_id, day, start_hr, start_min));
create table prereq (course_id varchar(8), prereq_id varchar(8), primary key (course_id, prereq_id));
create table grade_points(grade varchar(2), points Number(10,4), primary key (grade));
data file is
delete from prereq;
delete from time_slot;
delete from advisor;
delete from takes;
delete from student;
delete from teaches;
delete from section;
delete from instructor;
delete from course;
delete from department;
delete from classroom;
-- Classroom
insert into classroom values ('Packard', '101', '500');
insert into classroom values ('Painter', '514', '10');
insert into classroom values ('Taylor', '3128', '70');
insert into classroom values ('Watson', '100', '30');
insert into classroom values ('Watson', '120', '50');
-- Department
insert into department values ('Biology', 'Watson', '90000');
insert into department values ('Comp. Sci.', 'Taylor', '100000');
insert into department values ('Elec. Eng.', 'Taylor', '85000');
insert into department values ('Finance', 'Painter', '120000');
insert into department values ('History', 'Painter', '50000');
insert into department values ('Music', 'Packard', '80000');
insert into department values ('Physics', 'Watson', '70000');
-- Course
insert into course values ('BIO-101', 'Intro. to Biology', 'Biology', '4');
insert into course values ('BIO-301', 'Genetics', 'Biology', '4');
insert into course values ('BIO-399', 'Computational Biology', 'Biology', '3');
insert into course values ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.', '4');
insert into course values ('CS-190', 'Game Design', 'Comp. Sci.', '4');
insert into course values ('CS-315', 'Robotics', 'Comp. Sci.', '3');
insert into course values ('CS-319', 'Image Processing', 'Comp. Sci.', '3');
insert into course values ('CS-347', 'Database System Concepts', 'Comp. Sci.', '3');
insert into course values ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', '3');
insert into course values ('FIN-201', 'Investment Banking', 'Finance', '3');
insert into course values ('HIS-351', 'World History', 'History', '3');
insert into course values ('MU-199', 'Music Video Production', 'Music', '3');
insert into course values ('PHY-101', 'Physical Principles', 'Physics', '4');
-- Instructor
insert into instructor values ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
insert into instructor values ('12121', 'Wu', 'Finance', '90000');
insert into instructor values ('15151', 'Mozart', 'Music', '40000');
insert into instructor values ('22222', 'Einstein', 'Physics', '95000');
insert into instructor values ('32343', 'El Said', 'History', '60000');
insert into instructor values ('33456', 'Gold', 'Physics', '87000');
insert into instructor values ('45565', 'Katz', 'Comp. Sci.', '75000');
insert into instructor values ('58583', 'Califieri', 'History', '62000');
insert into instructor values ('76543', 'Singh', 'Finance', '80000');
insert into instructor values ('76766', 'Crick', 'Biology', '72000');
insert into instructor values ('83821', 'Brandt', 'Comp. Sci.', '92000');
insert into instructor values ('98345', 'Kim', 'Elec. Eng.', '80000');
-- Section
insert into section values ('BIO-101', '1', 'Summer', '2009', 'Painter', '514', 'B');
insert into section values ('BIO-301', '1', 'Summer', '2010', 'Painter', '514', 'A');
insert into section values ('CS-101', '1', 'Fall', '2009', 'Packard', '101', 'H');
insert into section values ('CS-101', '1', 'Spring', '2010', 'Packard', '101', 'F');
insert into section values ('CS-190', '1', 'Spring', '2009', 'Taylor', '3128', 'E');
insert into section values ('CS-190', '2', 'Spring', '2009', 'Taylor', '3128', 'A');
insert into section values ('CS-315', '1', 'Spring', '2010', 'Watson', '120', 'D');
insert into section values ('CS-319', '1', 'Spring', '2010', 'Watson', '100', 'B');
insert into section values ('CS-319', '2', 'Spring', '2010', 'Taylor', '3128', 'C');
insert into section values ('CS-347', '1', 'Fall', '2009', 'Taylor', '3128', 'A');
insert into section values ('EE-181', '1', 'Spring', '2009', 'Taylor', '3128', 'C');
insert into section values ('FIN-201', '1', 'Spring', '2010', 'Packard', '101', 'B');
insert into section values ('HIS-351', '1', 'Spring', '2010', 'Painter', '514', 'C');
insert into section values ('MU-199', '1', 'Spring', '2010', 'Packard', '101', 'D');
insert into section values ('PHY-101', '1', 'Fall', '2009', 'Watson', '100', 'A');
-- Teaches
insert into teaches values ('10101', 'CS-101', '1', 'Fall', '2009');
insert into teaches values ('10101', 'CS-315', '1', 'Spring', '2010');
insert into teaches values ('10101', 'CS-347', '1', 'Fall', '2009');
insert into teaches values ('12121', 'FIN-201', '1', 'Spring', '2010');
insert into teaches values ('15151', 'MU-199', '1', 'Spring', '2010');
insert into teaches values ('22222', 'PHY-101', '1', 'Fall', '2009');
insert into teaches values ('32343', 'HIS-351', '1', 'Spring', '2010');
insert into teaches values ('45565', 'CS-101', '1', 'Spring', '2010');
insert into teaches values ('45565', 'CS-319', '1', 'Spring', '2010');
insert into teaches values ('76766', 'BIO-101', '1', 'Summer', '2009');
insert into teaches values ('76766', 'BIO-301', '1', 'Summer', '2010');
insert into teaches values ('83821', 'CS-190', '1', 'Spring', '2009');
insert into teaches values ('83821', 'CS-190', '2', 'Spring', '2009');
insert into teaches values ('83821', 'CS-319', '2', 'Spring', '2010');
insert into teaches values ('98345', 'EE-181', '1', 'Spring', '2009');
-- Student
insert into student values ('00128', 'Zhang', 'Comp. Sci.', '102');
insert into student values ('12345', 'Shankar', 'Comp. Sci.', '32');
insert into student values ('19991', 'Brandt', 'History', '80');
insert into student values ('23121', 'Chavez', 'Finance', '110');
insert into student values ('44553', 'Peltier', 'Physics', '56');
insert into student values ('45678', 'Levy', 'Physics', '46');
insert into student values ('54321', 'Williams', 'Comp. Sci.', '54');
insert into student values ('55739', 'Sanchez', 'Music', '38');
insert into student values ('70557', 'Snow', 'Physics', '0');
insert into student values ('76543', 'Brown', 'Comp. Sci.', '58');
insert into student values ('76653', 'Aoi', 'Elec. Eng.', '60');
insert into student values ('98765', 'Bourikas', 'Elec. Eng.', '98');
insert into student values ('98988', 'Tanaka', 'Biology', '120');
-- Takes
insert into takes values ('00128', 'CS-101', '1', 'Fall', '2009', 'A');
insert into takes values ('00128', 'CS-347', '1', 'Fall', '2009', 'A-');
insert into takes values ('12345', 'CS-101', '1', 'Fall', '2009', 'C');
insert into takes values ('12345', 'CS-190', '2', 'Spring', '2009', 'A');
insert into takes values ('12345', 'CS-315', '1', 'Spring', '2010', 'A');
insert into takes values ('12345', 'CS-347', '1', 'Fall', '2009', 'A');
insert into takes values ('19991', 'HIS-351', '1', 'Spring', '2010', 'B');
insert into takes values ('23121', 'FIN-201', '1', 'Spring', '2010', 'C+');
insert into takes values ('44553', 'PHY-101', '1', 'Fall', '2009', 'B-');
insert into takes values ('45678', 'CS-101', '1', 'Fall', '2009', 'F');
insert into takes values ('45678', 'CS-101', '1', 'Spring', '2010', 'B+');
insert into takes values ('45678', 'CS-319', '1', 'Spring', '2010', 'B');
insert into takes values ('54321', 'CS-101', '1', 'Fall', '2009', 'A-');
insert into takes values ('54321', 'CS-190', '2', 'Spring', '2009', 'B+');
insert into takes values ('55739', 'MU-199', '1', 'Spring', '2010', 'A-');
insert into takes values ('76543', 'CS-101', '1', 'Fall', '2009', 'A');
insert into takes values ('76543', 'CS-319', '2', 'Spring', '2010', 'A');
insert into takes values ('76653', 'EE-181', '1', 'Spring', '2009', 'C');
insert into takes values ('98765', 'CS-101', '1', 'Fall', '2009', 'C-');
insert into takes values ('98765', 'CS-315', '1', 'Spring', '2010', 'B');
insert into takes values ('98988', 'BIO-101', '1', 'Summer', '2009', 'A');
insert into takes values ('98988', 'BIO-301', '1', 'Summer', '2010', null);
-- Advisor
insert into advisor values ('00128', '45565');
insert into advisor values ('12345', '10101');
insert into advisor values ('23121', '76543');
insert into advisor values ('44553', '22222');
insert into advisor values ('45678', '22222');
insert into advisor values ('76543', '45565');
insert into advisor values ('76653', '98345');
insert into advisor values ('98765', '98345');
insert into advisor values ('98988', '76766');
-- Time_slot
insert into time_slot values ('A', 'M', '8', '0', '8', '50');
insert into time_slot values ('A', 'W', '8', '0', '8', '50');
insert into time_slot values ('A', 'F', '8', '0', '8', '50');
insert into time_slot values ('B', 'M', '9', '0', '9', '50');
insert into time_slot values ('B', 'W', '9', '0', '9', '50');
insert into time_slot values ('B', 'F', '9', '0', '9', '50');
insert into time_slot values ('C', 'M', '11', '0', '11', '50');
insert into time_slot values ('C', 'W', '11', '0', '11', '50');
insert into time_slot values ('C', 'F', '11', '0', '11', '50');
insert into time_slot values ('D', 'M', '13', '0', '13', '50');
insert into time_slot values ('D', 'W', '13', '0', '13', '50');
insert into time_slot values ('D', 'F', '13', '0', '13', '50');
insert into time_slot values ('E', 'T', '10', '30', '11', '45 ');
insert into time_slot values ('E', 'R', '10', '30', '11', '45 ');
insert into time_slot values ('F', 'T', '14', '30', '15', '45 ');
insert into time_slot values ('F', 'R', '14', '30', '15', '45 ');
insert into time_slot values ('G', 'M', '16', '0', '16', '50');
insert into time_slot values ('G', 'W', '16', '0', '16', '50');
insert into time_slot values ('G', 'F', '16', '0', '16', '50');
insert into time_slot values ('H', 'W', '10', '0', '12', '30');
-- Prereq
insert into prereq values ('BIO-301', 'BIO-101');
insert into prereq values ('BIO-399', 'BIO-101');
insert into prereq values ('CS-190', 'CS-101');
insert into prereq values ('CS-315', 'CS-101');
insert into prereq values ('CS-319', 'CS-101');
insert into prereq values ('CS-347', 'CS-101');
insert into prereq values ('EE-181', 'PHY-101');
-- Grade_points
insert into grade_points values ('A+', 4.0);
insert into grade_points values ('A', 4.0);
insert into grade_points values ('A-', 3.7);
insert into grade_points values ('B+', 3.3);
insert into grade_points values ('B', 3.0);
insert into grade_points values ('B-', 2.7);
insert into grade_points values ('C+', 2.3);
insert into grade_points values ('C', 2.0);
insert into grade_points values ('C-', 1.7);
insert into grade_points values ('D+', 1.3);
insert into grade_points values ('D', 1.0);
insert into grade_points values ('D-', 0.7);
insert into grade_points values ('F', 0.0);
insert into grade_points values ('NP', 0.0);
insert into grade_points values ('U', 0.0);
Expected Query 1:
Srinivasan 3
Brandt 2
Crick 2
Katz 2
My result of Query 2:
Fall 2009 9
Spring 2009 3
Summer 2009 1
Expected Query 2:
Spring 2010 7
Spring 2009 3
Fall 2009 3
Expected Query 3:
12345 Shankar 4
45678 Levy 3
I cant give you the full solution because you still learning. But here are some guide lines.
First one:
Select name AS InstructorName, count(course_id) AS NumberOfCourses
from Teaches where name IN ....
Teaches doesn't have name, instead of WHERE you need JOIN to Instructor table
Second one:
You dont need filter WHERE year='2009' what they ask is you GROUP BY year, semester if you do GROUP BY semester then all Spring semester will be on the same group
Third one: Same as first. You need JOIN to STUDENTS
Related
I am studying SQL in my data science degree program, and the assignment is to perform SQL queries for the first time. In order to do that, I have to paste the SQL starter code in and run it. However, when I do, I hit an error that I can't solve. My class is online and my professor hasn't responded and, since this isn't part of the graded part of the assignment, I figured I'd try here to get help on this error.
Edit: It was pointed out that I didn't include all of the tables. Please see full code for all tables and all data. I was trying to avoid flooding this post with a lot of lines of code, but it seems I left out pertinent information. See below.
The tables were created with these blocks of code:
create table section (
course_id varchar(8),
sec_id varchar(8),
semester varchar(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
year numeric(4,0) check (year >1701 and year < 2100),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course on delete cascade,
foreign key (time_slot_id) references timeslot on delete no action,
foreign key (building, room_number) references classroom on delete set null
);
create table teaches (
ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section on delete cascade,
foreign key (ID) references instructor on delete set null
);
create table instructor (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2) check (salary >10000),
primary key (ID),
foreign key (dept_name) references department on delete set null
);
Then, I added data to instructor and section with the follow. Side note, I had to go in and manually set the time_slot_id as a primary key to avoid getting an error.
INSERT INTO classroom VALUES ('Innovation Hall', '101', 10);
INSERT INTO classroom VALUES ('Innovation Hall', '327', 30);
INSERT INTO classroom VALUES ('Exploratory Hall', '202', 20);
INSERT INTO classroom VALUES ('Research Hall', '302', 20);
INSERT INTO classroom VALUES ('Arts Center', '212B', 100);
INSERT INTO classroom VALUES ('Research Hall', '310', 30);
INSERT INTO classroom VALUES ('Innovation Hall', '121', 10);
INSERT INTO classroom VALUES ('Innovation Hall', '321', 20);
INSERT INTO classroom VALUES ('Arts Center', '120A', 100);
INSERT INTO classroom VALUES ('Exploratory Hall', '231', 20);
INSERT INTO classroom VALUES ('Innovation Hall', '220', 30);
INSERT INTO department VALUES ('CDS', 'Research Hall', 100000);
INSERT INTO department VALUES ('Music', 'Arts Center', 70000);
INSERT INTO department VALUES ('History', 'Arts Center', 50000);
INSERT INTO department VALUES ('Geography', 'Exploratory Hall', 60000);
INSERT INTO department VALUES ('Biology', 'Exploratory Hall', 85000);
INSERT INTO department VALUES ('Physics', 'Innovation Hall', 90000);
INSERT INTO department VALUES ('Math', 'Exploratory Hall', 55000);
INSERT INTO course VALUES ('CDS-101', 'Intro to CDS', 'CDS', 3);
INSERT INTO course VALUES ('CDS-130', 'Computing for Scientists', 'CDS', 3);
INSERT INTO course VALUES ('CDS-302', 'Databases', 'CDS', 3);
INSERT INTO course VALUES ('CDS-303', 'Data Mining', 'CDS', 3);
INSERT INTO course VALUES ('MUS-100', 'Fundamentals of Music', 'Music', 3);
INSERT INTO course VALUES ('HIS-101', 'History of Western Civilization', 'History', 3);
INSERT INTO course VALUES ('GGS-101', 'Major World Regions', 'Geography', 4);
INSERT INTO course VALUES ('BIO-101', 'Intro to Biology', 'Biology', 3);
INSERT INTO course VALUES ('BIO-301', 'Genetics', 'Biology', 4);
INSERT INTO course VALUES ('PHY-101', 'Intro to Physics', 'Physics', 3);
INSERT INTO course VALUES ('PHY-403', 'Quantum Mechanics', 'Physics', 4);
INSERT INTO course VALUES ('MAT-110', 'Probabilities', 'Math', 3);
INSERT INTO course VALUES ('MAT-114', 'Calculus', 'Math', 4);
INSERT INTO prereq VALUES ('CDS-130', 'CDS-101');
INSERT INTO prereq VALUES ('MAT-114', 'MAT-110');
INSERT INTO prereq VALUES ('PHY-403', 'PHY-101');
INSERT INTO prereq VALUES ('CDS-302', 'CDS-130');
INSERT INTO prereq VALUES ('CDS-302', 'PHY-403');
INSERT INTO prereq VALUES ('CDS-303', 'CDS-302');
INSERT INTO prereq VALUES ('CDS-303', 'MAT-114');
INSERT INTO instructor VALUES ('11111', 'Turing', 'CDS', 950000);
INSERT INTO instructor VALUES ('11112', 'Widom', 'CDS', 100000);
INSERT INTO instructor VALUES ('12121', 'Mozart', 'Music', 75000);
INSERT INTO instructor VALUES ('22222', 'Heraclitus', 'History', 65000);
INSERT INTO instructor VALUES ('12345', 'Eratosthenes', 'Geography', 55000);
INSERT INTO instructor VALUES ('54321', 'Crick', 'Biology', 88000);
INSERT INTO instructor VALUES ('33333', 'Newton', 'Physics', 80000);
INSERT INTO instructor VALUES ('44444', 'Euler', 'Math', 77000);
INSERT INTO instructor VALUES ('55555', 'Euclid', 'Math', 77000);
INSERT INTO timeslot VALUES ('A', 'M', 8, 0 , 9, 15);
INSERT INTO timeslot VALUES ('B', 'M', 10, 0 , 11, 15);
INSERT INTO timeslot VALUES ('C', 'T', 8, 0 , 9, 15);
INSERT INTO timeslot VALUES ('D', 'T', 10, 0 , 11, 15);
INSERT INTO timeslot VALUES ('E', 'T', 11, 15 , 12, 30);
INSERT INTO timeslot VALUES ('F', 'W', 9, 0 , 10, 15);
INSERT INTO timeslot VALUES ('G', 'R', 8, 0 , 10, 15);
INSERT INTO timeslot VALUES ('H', 'R', 15, 0 , 16, 15);
INSERT INTO timeslot VALUES ('I', 'F', 9, 0 , 10, 15);
INSERT INTO section VALUES ('CDS-101', '1', 'Fall', 2019, 'Research Hall', '310', 'A');
INSERT INTO section VALUES ('CDS-130', '1', 'Fall', 2019, 'Innovation Hall', '121', 'B');
INSERT INTO section VALUES ('CDS-302', '1', 'Fall', 2019, 'Innovation Hall', '327', 'C');
INSERT INTO section VALUES ('CDS-302', '2', 'Spring', 2019, 'Innovation Hall', '321', 'C');
INSERT INTO section VALUES ('MUS-100', '1', 'Fall', 2019, 'Arts Center', '120A', 'A');
INSERT INTO section VALUES ('HIS-101', '1', 'Fall', 2019, 'Arts Center', '212B', 'B');
INSERT INTO section VALUES ('GGS-101', '1', 'Fall', 2019, 'Exploratory Hall', '202', 'D');
INSERT INTO section VALUES ('BIO-101', '2', 'Fall', 2019, 'Exploratory Hall', '231', 'E');
INSERT INTO section VALUES ('PHY-403', '1', 'Spring', 2019, 'Innovation Hall', '220', 'F');
INSERT INTO section VALUES ('BIO-101', '2', 'Fall', 2020, 'Exploratory Hall', '231', 'C');
INSERT INTO section VALUES ('PHY-403', '1', 'Spring', 2020, 'Innovation Hall', '220', 'D');
I didn't have a problem. Finally, I tried to run this:
INSERT INTO teaches VALUES ('11111', 'CDS-101', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('11112', 'CDS-130', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('12121', 'MUS-100', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('54321', 'BIO-101', '2', 'Fall', 2019);
INSERT INTO teaches VALUES ('33333', 'PHY-403', '1', 'Spring', 2019);
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);
The error occurred on the last line. I always get:
Result: FOREIGN KEY constraint failed
At line 1: (Note: I tried re-running it by itself afterwards, thus the "Line 1")
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);
I checked over each constraint, looked at each column, and couldn't figure it out. I even went in and entered the data manually in the "Browse Data" tab within DB Browser Lite; while I was able to enter the first 4 values, when I try to enter anything for year, I get the same error in a popup box. Even when I click in the box, leave it blank, then try to click out, I get the same error. See image.
Please assist... I'm lost at this point and quite frustrated.
There are two things here:
the issue, and
how to work out what the issue is.
Let's start with 2).
The error message is:
Result: FOREIGN KEY constraint failed
At line 1:
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);
That tells us to look at the foreign keys defined on the teaches table.
There are 2:
ID references the ID in instructor table (aside: maybe call this instructor_id instead of ID, it'll make more sense in 6 weeks when you come back to look at it).
course_id, sec_id, semester, year reference the section table.
There is, in your data, an instructor with ID '44444', so that's probably not the problem.
But it doesn't look like there's a row in section with 'MAT-114', '2', 'Fall', 2019 and that is likely your problem.
Note also that there's not one for 'MTH-114' like in the image, either.
This is my first time asking a SQL related question. I am having a difficult time getting this query to work.
Basically, I have a database that has 3 tables Course, Faculty and Adjunct. This query is supposed to create a view named Top3Enrollment that returns FirstName, LastName, TotalStudents, and MaxEnrollment of the 3 faculty members with the largest total enrollment for their courses, along with the highest enrollment among the classes they teach.
When attempting to write the query I get an error with selecting the column FirstName
My query:
CREATE VIEW Top3Enrollment
AS
SELECT TOP 3 PERCENT
FirstName, LastName, SUM(Enrollment), MAX(Enrollment)
FROM
Faculty
JOIN
Course ON Faculty.Faculty_ID = Course.Faculty_ID
ORDER BY
MAX(Enrollment);
The error I get is:
Msg 8120, Level 16, State 1, Procedure Top3Enrollment, Line 3 [Batch Start Line 0]
Column 'Faculty.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My database structures:
CREATE TABLE Faculty
(
Faculty_ID VARCHAR(2),
LastName VARCHAR(20),
FirstName VARCHAR(20),
Department VARCHAR(10),
Campus VARCHAR(10)
);
INSERT INTO Faculty
VALUES ('1', 'Brown', 'Joe', 'Business', 'Kent'),
('2', 'Smith', 'John', 'Economics', 'Kent'),
('3', 'Jones', 'Sally', 'English', 'South'),
('4', 'Black', 'Bill', 'Economics', 'Deerwood'),
('5', 'Green', 'Gene', 'Business', 'South');
CREATE TABLE Course
(
Course_ID CHAR(2),
Ref_Number CHAR(5),
Faculty_ID VARCHAR(2),
Term Char(1),
BegDate SMALLDATETIME,
Enrollment INTEGER,
TotRev FLOAT
);
INSERT INTO Course
VALUES ('1', '12345', 'a', 'A', '2016-01-08 00:00:00', 24, 12345.32 ),
('2', '54321', '3', 'B', '2016-02-04 00:00:00', 18, 21435.51 ),
('3', '13524', '1', 'B', '2016-02-04 00:00:00', 7, 1256.67 ),
('4', '24653', '1', 'C', '2016-03-04 00:00:00', 29, 54421.11 ),
('5', '98765', '5', 'A', '2016-01-08 00:00:00', 35, 246753.23),
('6', '14862', '2', 'B', '2016-02-04 00:00:00', 14, 9876.87),
('7', '96032', '1', 'C', '2016-03-04 00:00:00', 8, 863159.31),
('8', '81256', '5', 'A', '2016-01-08 00:00:00', 5, 98762.14),
('9', '64321', '2', 'C', '2016-03-04 00:00:00', 23, 2965.98),
('10','90908', 'a', 'A', '2016-01-08 00:00:00', 45, 91724.02),
('11','90908', '3', 'A', '2016-01-08 00:00:00', 23, 73725.77),
('12','90908', '3', 'A', '2016-01-08 00:00:00', 16, 84224.29),
('13','90908', 'b', 'A', '2016-01-08 00:00:00', 13, 42719.82);
CREATE TABLE Adjuncts
(
Faculty_ID Char(2),
LastName VARCHAR(20),
FirstName VARCHAR(20),
Department VARCHAR(10),
Campus VARCHAR(10)
);
INSERT INTO Adjuncts
VALUES ('a', 'Rogers', 'Aaron', 'Business', 'Kent'),
('b', 'Manning', 'Peyton', 'Economics', 'North'),
('c', 'Drew', 'Maurice', 'English', 'Cecil'),
('d', 'Griffin', 'Robert', 'Music', 'Deerwood'),
('e', 'Goodell', 'Roger', 'Economics', 'South'),
('f', 'Vilma', 'Jonathan', 'Business', 'Kent');
Note:
I Understand I cannot have Order By but I wouldn't know what else to use
Added the database code
When you use aggregate functions like sum and max together with other columns you need to group your aggregations by those other columns.
Add GROUP BY like this
SELECT TOP 3 PERCENT FirstName, LastName, SUM(Enrollment), MAX(Enrollment)
FROM Faculty
JOIN Course ON Faculty.Faculty_ID = Course.Faculty_ID
GROUP BY FirstName, LastName
ORDER BY MAX(Enrollment);
My code:
drop table if exists HSstudents;
create table HSstudents
(
HSsID int,
vNAAM text,
aNAAM text,
LT int,
GM float
);
insert into HSstudents values (1, 'Thomas', 'Jansen', 18, 7.0);
insert into HSstudents values (2, 'Jesse', 'Bakker', 19, 6.5);
insert into HSstudents values (3, 'Tom', 'Smit', 20, 7.1);
insert into HSstudents values (4, 'Jelle', 'Visser', 17, 9.6);
insert into HSstudents values (5, 'Sem', 'Dekker', 17, 8.1);
insert into HSstudents values (6, 'Anna', 'Peters', 18, 6.8);
insert into HSstudents values (7, 'Michelle', 'Hendriks', 19, 8.2);
insert into HSstudents values (8, 'Senna', 'Mulder', 20, 5.9);
insert into HSstudents values (9, 'Sven', 'Linden', 21, 6.0);
insert into HSstudents values (10, 'Ilse', 'Jacobs', 21, 7.5);
insert into HSstudents values (11, 'Harm', 'Schouten', 19, 7.0);
insert into HSstudents values (12, 'Emma', 'Dijkstra', 18, 8.1);
drop table if exists students;
create table students
(
sID int,
vNAAM text,
aNAAM text,
LT int
);
insert into students values (1, 'Thomas', 'Jansen', 18);
insert into students values (2, 'Jesse', 'Bakker', 19);
insert into students values (3, 'Tom', 'Smit', 20);
insert into students values (4, 'Jelle', 'Visser', 17);
insert into students values (5, 'Sem', 'Dekker', 17);
insert into students values (6, 'Anna', 'Peters', 18);
insert into students values (7, 'Michelle', 'Hendriks', 19);
insert into students values (8, 'Senna', 'Mulder', 20);
insert into students values (9, 'Sven', 'Linden', 21);
insert into students values (10, 'Ilse', 'Jacobs', 21);
insert into students values (11, 'Harm', 'Schouten', 19);
insert into students values (12, 'Emma', 'Dijkstra', 18);
drop table if exists applications;
create table applications
(
sID int,
aPROV text,
sPROV text,
taal text
);
insert into applications values (1, 'Overijssel', 'Drenthe', 'HTML');
insert into applications values (2, 'Gelderland', 'Overijssel', 'CSS');
insert into applications values (3, 'Groningen', 'Flevoland', 'CSS');
insert into applications values (4, 'Overijssel', 'Zuid-Holland', 'SQL');
insert into applications values (5, 'Noord-Holland', 'Drenthe', 'C#');
insert into applications values (6, 'Flevoland', 'Groningen', 'C#');
insert into applications values (7, 'Limburg', 'Groningen', 'JAVA');
insert into applications values (8, 'Limburg', 'Limburg', 'JAVASCRIPT');
insert into applications values (9, 'Drenthe', 'Noord-Brabant', 'CSS');
insert into applications values (10, 'Drenthe', 'Zeeland', 'Python');
insert into applications values (11, 'Zuid-Holland', 'Friesland', 'C++');
insert into applications values (12, 'Zeeland', 'Friesland', 'JAVA');
select
S.sID, S.vNAAM, S.aNAAM, S.LT, aPROV, sPROV, taal
from
HSstudents HS, students S, applications A
where
HSstudents.HSsID = students.sID
This results in an error
Code: 1054. Unknown column 'HSstudents.HSsID' in 'where clause'
How? Shouldn't it just work?
WHERE clause should follow the remane on the FROM clause:
where HS.HSsID = S.sID
I am trying to write a query to check how many seats have been filled and how many seats are left in a row for a movie in a screen but after hours of trying I'm still unable to get my head around it, I have tried many queries altering the where clause even sub queries, every time I get some error :(
The query I am using this as follows:
select
10 as [total seats in a row],
perName as [movie name],
seatRow as [seat row],
(filledSeats - 10) as [seats left in a row]
count(s.seatNo) filledSeats
FROM
seats as s inner join
tickets as t on s.seatNo = t.seatNo inner join
performances as p on t.perID = p.perID
where
screenNo = '2' AND
perName = '50 shades of Grey'
group by
perName
But even this one is giving me error
the sample data is as follows:
CREATE TABLE seats (
seatNo int IDENTITY (1,1) not null,
screenNo int,
screen varchar (20),
seatType Varchar (10),
seatRow varchar (5),
PRIMARY KEY (seatNo),
FOREIGN KEY (screenNo) REFERENCES screens
);
insert into seats values ('1','3', 'Normal', 'A');
insert into seats values ('1','3', 'Normal', 'B');
insert into seats values ('1','3', 'VIP', 'D');
insert into seats values ('1','1', 'Normal', 'F');
insert into seats values ('1','3', 'VIP', 'E');
insert into seats values ('1','2', 'VIP', 'C');
insert into seats values ('1','1', 'VIP', 'C');
insert into seats values ('2','2', 'Normal', 'D');
insert into seats values ('2','1', 'VIP', 'F');
insert into seats values ('2','2', 'Normal', 'B');
insert into seats values ('2','1', 'Normal', 'B');
insert into seats values ('2','2', 'VIP', 'B');
insert into seats values ('2','2', 'Normal', 'A');
insert into seats values ('3','2', 'VIP', 'B');
insert into seats values ('2','1', 'Normal', 'C');
insert into seats values ('3','3', 'Normal', 'E');
insert into seats values ('3','2', 'VIP', 'C');
insert into seats values ('3','3', 'VIP', 'A');
insert into seats values ('3','1', 'VIP', 'E');
insert into seats values ('3','1', 'Normal', 'D');
CREATE TABLE performances(
perID int IDENTITY (1,1)NOT NULL,
perName varchar (50),
perTime Time,
perDate Date,
perType varchar (20),
duration varchar (10),
screenNo int,
rating varchar (10),
location varchar (10),
PRIMARY KEY (perID),
);
insert into performances values ('50 Shades of Grey', '12:00', '2015-03-08', 'Movie', '2hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '15:00', '2015-02-20', 'Movie', '2hrs', '2', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '18:00', '2015-02-26', 'Movie', '2hrs', '3', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '21:00', '2015-03-28', 'Movie', '2hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '12:00', '2015-03-26', 'Movie', '2:30hrs', '2', '18', 'Chelmsford');
insert into performances values ('Paddington', '15:00', '2015-03-03', 'Movie', '2:30hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '18:00', '2015-02-02', 'Movie', '2:3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '21:00', '2015-03-31', 'Movie', '2:30hrs', '1', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '12:00', '2015-02-25', 'Movie', '3hrs', '2', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '15:00', '2015-02-06', 'Movie', '3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Shakespeare', '18:00', '2015-02-13', 'Movie', '3hrs', '3', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '21:00', '2015-01-09', 'Movie', '3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '12:00', '2015-01-11', 'Movie', '2:3hrs', '1', '18', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '15:00', '2015-01-19', 'Movie', '2:30hrs', '2', '18', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '18:00', '2015-02-06', 'Movie', '2:3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '21:00', '2015-01-02', 'Movie', '2:3hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '12:00', '2015-01-06', 'Movie', '2hrs', '1', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '15:00', '2015-03-28', 'Movie', '2hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '18:00', '2015-02-09', 'Movie', '2hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '21:00', '2015-02-06', 'Movie', '2hrs', '2', '18', 'Chelmsford');
CREATE TABLE bookings(
bookingID int IDENTITY (1,1) NOT NULL,
customerID int ,
clerkID varchar (20) ,
perID int,
ticketBooked int,
bookingDate Date,
bookingTime time,
bookingType varchar(20),
totalCost decimal(5,2),
PRIMARY KEY (bookingID),
FOREIGN KEY (customerID) REFERENCES customers,
FOREIGN Key (clerkID) REFERENCES clerks,
);
insert into bookings values (16, 'clerk04', 1, 2, '2015-02-24', '12:00', 'Normal', '20');
insert into bookings values (2, 'clerk02', 20,3, '2015-02-25', '15:00', 'Advance', '34');
insert into bookings values (10, 'clerk04', 3,5, '2015-02-17', '18:00', 'Advance', '75');
insert into bookings values (5, 'clerk01', 19,2, '2015-01-24', '21:00', 'Advance', '30');
insert into bookings values (13, 'clerk01', 4,1, '2015-02-05', '12:00', 'Normal', '10');
insert into bookings values (20, 'clerk04', 5,1,'2015-01-26', '15:00', 'Normal', '12');
insert into bookings values (4, 'clerk03', 6, 3,'2015-02-09', '18:00', 'Advance', '30');
insert into bookings values (1, 'clerk03', 2, 1,'2015-01-03', '21:00', 'Advance', '15');
insert into bookings values (17, 'clerk04', 3, 1,'2015-02-09', '12:00', 'Normal', '12');
insert into bookings values (9, 'clerk02', 18,2,'2015-02-02', '15:00', 'Advance', '20');
insert into bookings values (11, 'clerk01', 6,3,'2015-02-10', '18:00', 'Advance', '45');
insert into bookings values (18, 'clerk01',1 ,1,'2015-02-06', '21:00', 'Normal', '12');
insert into bookings values (8, 'clerk05', 5,2,'2015-01-13', '12:00', 'Advance', '20');
insert into bookings values (6, 'clerk02', 7,2,'2015-02-05', '15:00', 'Advance', '20');
insert into bookings values (10, 'clerk03', 8,1,'2015-02-14', '18:00', 'Normal', '15');
insert into bookings values (14, 'clerk01', 9,2,'2015-01-19', '21:00', 'Normal', '20');
insert into bookings values (7, 'clerk05', 10, 3,'2015-01-29', '12:00', 'Advance','34');
insert into bookings values (3, 'clerk01', 12, 1,'2015-02-18', '15:00', 'Normal', '12');
insert into bookings values (13, 'clerk02', 15,3,'2015-01-08', '18:00', 'Normal', '45');
insert into bookings values (19, 'clerk03', 6, 2,'2015-01-27', '21:00', 'Normal', '30');
CREATE Table tickets (
ticketNo int IDENTITY (1,1) Not Null,
bookingID int,
perID int,
seatNo int,
ticketType varchar(10),
ticketDate date,
ticketPrice decimal (4,2),
screen int,
perTime time,
PRIMARY KEY (ticketNo),
FOREIGN KEY (bookingID) REFERENCES bookings,
FOREIGN KEY (perID) REFERENCES performances,
FOREIGN KEY (seatNo) REFERENCES seats,
);
insert into tickets values (20, 1, 1,'Student', '2015-01-12', '10 ', '1', '15:00');
insert into tickets values (19, 7, 2,'VIP', '2015-01-11', '15', '2', '21:00');
insert into tickets values (18, 1, 3,'Adult', '2015-02-21', '12', '2', '15:00');
insert into tickets values (17, 19, 4,'Adult', '2015-03-25', '12', '1', '18:00');
You haven't shown any schema (or data) for tickets, and bookings although not used in your query has its schema and data shown, so I can only guess at what you want.
That said, this is your query with (I believe) syntax problems corrected:
select
10 as [total seats in a row],
perName as [movie name],
seatRow as [seat row],
count(s.seatNo) - 10 as [seats left in a row],
count(s.seatNo) filledSeats
FROM seats as s
join tickets as t on s.seatNo = t.seatNo
join performances as p on t.perID = p.perID
where screenNo = '2'
AND perName = '50 shades of Grey'
group by perName, seatRow
Regarding the error you report, you're getting that error because once you give a table an alias, the table is effectively renamed for the query to that alias - so you can not refer to the full table name.
Change performances.perName to p.perName.
Solving problems; step by step:
After (filledSeats - 10) as [seats left in a row] a , is missed.
filledSeats is not a valid field in your tables, So where is it from?
If it is the next field it should be count(s.seatNo) again, because you can't use aliases in your current query
screenNo is not a valid field also in your query, because it needs a prefix that determines that it's coming from where like s.screenNo or p.screenNo.
when you have a field like seatRow after select without any aggregation function you should call it after group by too.
So below query will have no error; but needs some edits:
select
10 as [total seats in a row],
perName as [movie name],
seatRow as [seat row],
(count(s.seatNo) - 10) as [seats left in a row],
count(s.seatNo) as filledSeats
FROM
seats as s inner join
tickets as t on s.seatNo = t.seatNo inner join
performances as p on t.perID = p.perID
where
p.screenNo = '2' AND
perName = '50 shades of Grey'
group by
perName, seatRow
So if you use it your result is:
total seats in a row | movie name | seat row | seats left in a row | filledSeats
---------------------+----------------------+----------+---------------------+------------
10 | 50 Shades of Grey | F | -9 | 1
And if you change (count(s.seatNo) - 10) to (10 - count(s.seatNo)) you will have 9 instead of -9.
You can try something like this
SELECT
s.seatType,
COUNT(*) as [total seats in a row],
perName as [movie name],
seatRow as [seat row],
COUNT(*) - SUM(CASE WHEN t.perID IS NOT NULL THEN 1 ELSE 0 END) as [seats left in a row],
SUM(CASE WHEN t.perID IS NOT NULL THEN 1 ELSE 0 END) filledSeats
FROM performances as p
INNER JOIN seats as s
ON s.screenNo = p.screenNo
LEFT JOIN
(
SELECT b.perid ,t.seatNo
FROM bookings b
INNER JOIN tickets as t
ON t.bookingid = b.bookingid
)T
ON s.seatNo = t.seatNo
AND T.perid = p.perid
WHERE p.perid = 1
GROUP BY perName, s.seatType,seatRow
You can look at this SQL Fiddle
I changed some of the input data as the related IDs were incorrect. The schema has redundant columns.
your seats table has a reference to a screen table and also a screen column apart from the FK screenNo.
Tickets has a FK to performance as well as booking (which in turn has a FK to performance). These columns look redundant as well (ticketDate, screen and pertime)
Location in performance table looks to be an attribute of screen or another location specific table which should be referred by screen.
I am looking to fine the number of tickets booked for a performance + total revenue of that performance compared to the total revenue of all the performances. I have managed to count the number of tickets booked but its the full query doesnt work and giving me error.
sample result will be following for example:
`tickets booked = 9
total revenue of the performance = xxx$
total revenue of all the performances = xxx$
the query i am using is this:
SET #performancerevenue = '50 Shades of Grey';
select distinct P.perName as Performance,
(select COUNT(ticketBooked) from bookings as B
inner join performances as P on
B.perID = p.perID where perName = #performancerevenue) as
[Number of Ticket Booked ],
CAST(AVG((T.ticketPrice*left(B.totalCost,1))) over() as decimal(6,2)) as[Total Revenue from the performance (£)],
CAST(AVG(convert(decimal(5,2),(left(PY.totalAmount,1)))) over() as decimal(5,2)) as [Total revenue]
from performances as P
inner join tickets as T
on T.perID = P.perID
inner join bookings as B
on T.bookingID = B.bookingID
inner join payments as PY
where P.perName = #performancerevenue
The table and Sample Data is as follows :
CREATE TABLE bookings(
bookingID int IDENTITY (1,1) NOT NULL,
customerID int ,
clerkID varchar (20) ,
perID int,
ticketBooked int,
bookingDate Date,
bookingTime time,
bookingType varchar(20),
totalCost decimal(5,2),
PRIMARY KEY (bookingID),
FOREIGN KEY (customerID) REFERENCES customers,
FOREIGN Key (clerkID) REFERENCES clerks,);
insert into bookings values (16, 'clerk04', 1, 2, '2015-02-24', '12:00', 'Normal', '20');
insert into bookings values (2, 'clerk02', 20,3, '2015-02-25', '15:00', 'Advance', '34');
insert into bookings values (10, 'clerk04', 3,5, '2015-02-17', '18:00', 'Advance', '75');
insert into bookings values (5, 'clerk01', 19,2, '2015-01-24', '21:00', 'Advance', '30');
insert into bookings values (13, 'clerk01', 4,1, '2015-02-05', '12:00', 'Normal', '10');
insert into bookings values (20, 'clerk04', 5,1,'2015-01-26', '15:00', 'Normal', '12');
insert into bookings values (4, 'clerk03', 6, 3,'2015-02-09', '18:00', 'Advance', '30');
insert into bookings values (1, 'clerk03', 2, 1,'2015-01-03', '21:00', 'Advance', '15');
insert into bookings values (17, 'clerk04', 3, 1,'2015-02-09', '12:00', 'Normal', '12');
CREATE TABLE performances(
perID int IDENTITY (1,1)NOT NULL,
perName varchar (50),
perTime Time,
perDate Date,
perType varchar (20),
duration varchar (10),
screenNo int,
rating varchar (10),
location varchar (10),
PRIMARY KEY (perID),);
insert into performances values ('50 Shades of Grey', '12:00', '2015-03-08', 'Movie', '2hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '15:00', '2015-02-20', 'Movie', '2hrs', '2', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '18:00', '2015-02-26', 'Movie', '2hrs', '3', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '21:00', '2015-03-28', 'Movie', '2hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '12:00', '2015-03-26', 'Movie', '2:30hrs', '2', '18', 'Chelmsford');
insert into performances values ('Paddington', '15:00', '2015-03-03', 'Movie', '2:30hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '18:00', '2015-02-02', 'Movie', '2:3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '21:00', '2015-03-31', 'Movie', '2:30hrs', '1', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '12:00', '2015-02-25', 'Movie', '3hrs', '2', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '15:00', '2015-02-06', 'Movie', '3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Shakespeare', '18:00', '2015-02-13', 'Movie', '3hrs', '3', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '21:00', '2015-01-09', 'Movie', '3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '12:00', '2015-01-11', 'Movie', '2:3hrs', '1', '18', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '15:00', '2015-01-19', 'Movie', '2:30hrs', '2', '18', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '18:00', '2015-02-06', 'Movie', '2:3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '21:00', '2015-01-02', 'Movie', '2:3hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '12:00', '2015-01-06', 'Movie', '2hrs', '1', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '15:00', '2015-03-28', 'Movie', '2hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '18:00', '2015-02-09', 'Movie', '2hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '21:00', '2015-02-06', 'Movie', '2hrs', '2', '18', 'Chelmsford');
CREATE TABLE payments(
paymentID int IDENTITY (1,1) not null,
bookingID int,
totalAmount decimal (5,2),
paymentType varchar (20),
paymentDate Date,
PRIMARY KEY (paymentID),
FOREIGN KEY (bookingID) REFERENCES bookings,);
insert into payments values (1, 20, 'Cash', '2015-01-20');
insert into payments values (10, 20, 'Cash', '2015-01-17');
insert into payments values (12, 12, 'Credit/Debit Card', '2015-03-31');
insert into payments values (5, 10, 'Cash', '2015-01-08');
insert into payments values (18, 12, 'Cash', '2015-03-22');
insert into payments values (3, 75, 'Credit/Debit Card', '2015-02-21');
insert into payments values (2, 34, 'Credit/Debit Card', '2015-03-26');
insert into payments values (9, 12, 'Cash', '2015-03-26');
insert into payments values (13, 20, 'Credit/Debit Card', '2015-01-23');
insert into payments values (16, 20, 'Credit/Debit Card', '2015-03-08');
insert into payments values (11, 45, 'Credit/Debit Card', '2015-02-21');
insert into payments values (4, 30, 'Credit/Debit Card', '2015-02-08');
insert into payments values (7, 30, 'Cash', '2015-01-20');
insert into payments values (20, 30, 'Cash', '2015-02-26');
insert into payments values (14, 20, 'Credit/Debit Card', '2015-03-17');
insert into payments values (19, 45, 'Cash', '2015-01-10');
insert into payments values (6, 12, 'Credit/Debit Card', '2015-02-02');
insert into payments values (20, 30, 'Cash', '2015-02-01');
insert into payments values (17, 34, 'Credit/Debit Card', '2015-03-27');
insert into payments values (15, 15, 'Credit/Debit Card', '2015-02-14');
i have been trying so hard to get the result working for the last 4 hours but it just giving me errors I will really appreciate your help :(
thanks
In my opinion you can get the desired result by following way:
SELECT DISTINCT
P.perName as [Perf],
SUM(B.ticketBooked) OVER(PARTITION BY P.perName) as [BookedTickets],
SUM(B.totalCost) OVER(PARTITION BY P.perName) as [PerfRevenue],
SUM(B.totalCost) OVER() as [TotalRevenue]
from bookings as B
inner join performances as P on B.perID = p.perID
This query results something like this:
------------------------------------------------------------
Perf BookedTickets PerfRevenue TotalRevenue
------------------+---------------+------------+------------
50 Shades of Grey 15 196.00 238.00
Paddington 4 42.00 238.00
And you can add any outer query for this table for additional filtering and calculations.