Pārlūkot izejas kodu

fixed compile errors (errors that present when running on empty datasets)

tsdedhar 6 gadi atpakaļ
vecāks
revīzija
1b3a8fb245
2 mainītis faili ar 37 papildinājumiem un 43 dzēšanām
  1. 32 34
      a2/a2.sql
  2. 5 9
      a2/test.sql

+ 32 - 34
a2/a2.sql

@@ -1,3 +1,4 @@
+
 -- Current term is 1185
 
 /*
@@ -5,6 +6,7 @@ The student number and name of second year students who have obtained
 a grade lower than 65 in at least two courses in a department with the
 name “computer science”.
 */
+-- ran on empty
 select distinct sname, snum from student where year = 2 and snum in
 (
   select snum from mark m1 where grade < 65 and deptcode = 'CS' and snum in
@@ -17,6 +19,7 @@ select distinct sname, snum from student where year = 2 and snum in
 The number and name of professors who are not in the pure math (PM)
 department, and who are teaching CS245 for the first time.
 */
+-- ran on empty
 select distinct pnum, pname from professor where deptcode not in
 (
   select deptcode from department where deptname = 'PM'
@@ -32,11 +35,10 @@ select distinct pnum, pname from professor where deptcode not in
 The number, name and year of each student who has obtained a grade in
 CS240 that is within 3 marks of the highest ever grade recorded for that
 course.
--- Got an error for having not the same # of args on the >= (i think). ACTUALLY THE ISSUE IS WITH AN "=" specifically
 */
-select distinct snum, sname, year from student where snum in
+select s.snum, s.sname, s.year from student s where s.snum in
 (
-  select snum, grade from mark where deptcode = 'CS' and cnum = 240 and grade >= ((select grade from mark where deptcode = 'CS' and cnum = 240 order by grade desc limit 1) - 3)
+  select snum from mark where deptcode = 'CS' and cnum = 240 and grade >= ((select grade from mark where deptcode = 'CS' and cnum = 240 order by grade desc limit 1) - 3)
 );
 
 /*
@@ -45,13 +47,12 @@ have a final grade of at least 85 in every computer science course that
 they have taken, and who have always been taught by a professor in the
 computer science (CS) department.
 */
--- another issue with an unbalanced = statement
-select distinct snum, sname from student where year > 2 and snum not in
+select snum, sname from student where year > 2 and snum not in
 (
-  select snum, grade from mark where deptcode = 'CS' and grade < 85
+  select snum from mark where deptcode = 'CS' and grade < 85
 ) and snum not in
 (
-  select e.snum, e.deptcode, e.cnum, e.term, e.section from enrollment e where exists
+  select e.snum from enrollment e where exists
   (
     select c.pnum, c.deptcode, c.cnum, c.term, c.section from class c where
     c.deptcode = e.deptcode and c.cnum = e.cnum and c.term = e.term and c.section = e.section and c.pnum not in
@@ -69,7 +70,7 @@ select deptname from department where deptcode not in
 (
   select p.deptcode, p.pnum from professor p where pnum in
   (
-    select pnum from class where term = 1185 and deptcode != p.deptcode and pnum = p.pnum
+    select pnum, term, deptcode from class where term = 1185 and deptcode != p.deptcode and pnum = p.pnum
   )
 ) order by deptname;
 
@@ -111,21 +112,15 @@ in a particular term the second also teaches a class for the same course
 in the same term. Report a professor number and name for both the
 professors.
 */
--- not working, c.deptcode not valid somewhere
-select pnum, pname, pnum2, pname2 from professor, (select pnum as pnum2, pname as pname2 from professor where pnum in
-    (
-      select pnum from class where deptcode = c.deptcode and cnum = c.cnum and term = c.term and pnum != c.pnum
-    )) as subquery where pnum in
-(
-  select c.pnum, c.deptcode, c.cnum, c.term from class c where exists
-  (
-    select pnum as pnum2, pname as pname2 from professor where pnum in
-    (
-      select pnum from class where deptcode = c.deptcode and cnum = c.cnum and term = c.term and pnum != c.pnum
-    )
-  )
-);
-
+select p1.pnum, p1.pname, p2.pnum, p2.pname from professor p1, professor p2
+ where p1.pnum != p2.pnum and not exists
+ (
+  select * from class c, class c2 where
+    c.pnum = p1.pnum and c2.pnum = p2.pnum and (
+    c.deptcode != c2.deptcode or
+    c.term != c2.term or
+    c.cnum != c2.cnum)
+ )
 -- NOW CAN USE AGGREGATION
 
 /*
@@ -142,14 +137,13 @@ The percentage of professors in pure math who have always taught no
 more than a single course in any given term. (Note that a percentage
 should be a number between 0 and 100.)
 */
--- not liking the formatting
-select (select count(*) from professor p where deptcode = 'PM' and pnum in
+(select count(*) from professor p where deptcode = 'PM' and pnum in
 (
-  select c.pnum, c.deptcode, c.cnum, c.term, c.section from class c where c.pnum = p.pnum and c.pnum not in
+  select c.pnum from class c where c.pnum = p.pnum and c.pnum not in
   (
-    select c1.pnum, c1.deptcode, c1.cnum, c1.term, c1.section from class c1 where pnum = p.pnum and c1.term = c.term and (c1.deptcode != c.deptcode or c1.cnum != c.cnum)
+    select c1.pnum from class c1 where pnum = p.pnum and c1.term = c.term and (c1.deptcode != c.deptcode or c1.cnum != c.cnum)
   )
-))
+)
 / (select count(*) from professor where deptcode = 'PM');
 
 /*
@@ -178,21 +172,21 @@ The ratio of professors in pure math (PM) to professors in applied math
 (AM) who have taught a class in which the average grade obtained in the
 class was greater than 77.
 */
-select (select count(*) from professor p where deptcode = 'PM' and pnum in
+select count(*) from professor p where deptcode = 'PM' and pnum in
 (
-  (select c.pnum, c.deptcode, c.cnum, c.term, c.section from class c where
+  (select c.pnum from class c where
   (
     select avg(grade) from mark where c.deptcode = deptcode and c.cnum = cnum and c.term = term and c.section = section
   ) > 77)
-))
+)
 /
 (select count(*) from professor p where deptcode = 'AM' and pnum in
 (
-  select c.pnum, c.deptcode, c.cnum, c.term, c.section from class c where
+  select c.pnum from class c where
   (
     select avg(grade) from mark where c.deptcode = deptcode and c.cnum = cnum and c.term = term and c.section = section
   ) > 77
-)) as ratio;
+));
 
 /*
 For the current term, report how many courses there are in the schedule
@@ -202,4 +196,8 @@ indicates that there are 5 courses with a single class (section), 4 courses
 with 2 classes, and 1 course with 5 classes scheduled in the curent term.
 */
 -- ran on empty dataset
-select count(*), count(distinct section) from schedule where term = 1185;
+with eachclass (sectioncount) as
+(select count(*) from class
+  where term = 1185
+  group by (deptcode, cnum))
+select count(*), sectioncount from eachclass group by sectioncount;

+ 5 - 9
a2/test.sql

@@ -1,10 +1,6 @@
 -- run this to use test: db2 -tvmf test.sql
-select (select pname from professor where pnum = c.pnum), c.pnum, c.cnum, c.term, c.section,
-  (select count(distinct snum) from mark where snum in
-  (
-    select snum from student where year between 3 and 4
-  ) and deptcode = c.deptcode and cnum = c.cnum and term = c.term and section = c.section)
-from class c where pnum in
-(
-  select pnum from professor where deptcode = 'PM'
-) and term != 1185;
+with eachclass (sectioncount) as
+(select count(*) from class
+  where term = 1185
+  group by (deptcode, cnum))
+select count(*), sectioncount from eachclass group by sectioncount;