瀏覽代碼

added q11

tarfeef101 6 年之前
父節點
當前提交
7baae89a65
共有 1 個文件被更改,包括 51 次插入4 次删除
  1. 51 4
      a2/a2.sql

+ 51 - 4
a2/a2.sql

@@ -7,7 +7,7 @@ name “computer science”.
 */
 select distinct sname, snum from student where year = 2 and snum in
 (
-  select snum as old_snum from mark where grade < 65 and deptcode in
+  select snum from mark m1 where grade < 65 and deptcode in
   (
     select deptcode from department where deptname = "computer science"
   ) /*having count(distinct section) > 1*/ and snum in
@@ -15,7 +15,7 @@ select distinct sname, snum from student where year = 2 and snum in
     select snum from mark where grade < 65 and deptcode in
     (
       select deptcode from department where deptname = "computer science"
-    ) and snum != old_snum
+    ) and snum != m1.snum
   )
 )
 
@@ -41,7 +41,7 @@ course.
 */
 select distinct snum, sname, year from student where snum in
 (
-  select snum, grade from mark where deptcode = "CS" and cnum = 240 and snum >= (select grade from mark where deptcode = "CS" and cnum = 240 order by grade desc limit 1)
+  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)
 )
 
 /*
@@ -84,7 +84,30 @@ grades obtained and the maximum grades obtained. In addition to these
 four values, each result should include the number and name of each professor,
 as well as the identifying attributes for each class.
 */
+with combineTable (grade, pname, pnum, deptcode, cnum, term, section) as
+(select m.grade as grade, p.pname as pname, p.pnum as pnum,
+        cl.deptcode as deptcode, cl.cnum as cnum, cl.term as term, cl.section as section
+  from mark m, enrollment e, class cl, course co, professor p
+  where m.snum = e.snum and m.deptcode = e.deptcode and m.cnum = e.cnum and m.term = e.term and m.section = e.section
+      and
+      cl.deptcode = e.deptcode and cl.cnum = e.cnum and cl.term = e.term and cl.section = e.section
+      and
+      cl.deptcode = co.deptcode and cl.cnum = co.cnum
+      and
+      cl.pnum = p.pnum)
 
+select
+(select grade from mark where deptcode = c1.deptcode and cnum = c1.cnum and term = c1.term and section = c1.section order by grade desc limit 1)
+,
+(select grade from mark where deptcode = c1.deptcode and cnum = c1.cnum and term = c1.term and section = c1.section order by grade asc limit 1)
+,
+(select grade from mark where deptcode = c2.deptcode and cnum = c2.cnum and term = c2.term and section = c2.section order by grade desc limit 1)
+,
+(select grade from mark where deptcode = c2.deptcode and cnum = c2.cnum and term = c2.term and section = c2.section order by grade asc limit 1),
+c1.cnum, c1.term, c1.pnum, c1.pname, c1.deptcode, c1.section, c1.pnum,
+c2.cnum, c2.term, c2.pnum, c2.pname, c2.deptcode, c2.section, c2.pnum
+from combineTable c1, combineTable c2
+where c1.term = c2.term and c1.deptcode = c2.deptcode and c1.cnum = c2.cnum and c1.pnum != c2.pnum
 
 /*
 Pairs of distinct professors such that whenever the first one teaches a class
@@ -93,6 +116,7 @@ in the same term. Report a oprofessor number and name for both the
 professors.
 */
 
+
 -- NOW CAN USE AGGREGATION
 
 /*
@@ -100,8 +124,11 @@ The course number and total enrollment count for all of its classes of each
 course. Also, include only those course numbers for courses with a total
 enrollment count among the three lowest such counts.
 */
+select course.cnum, count(class) from course, class, where count(class)
+
+
 
-select course.cname, COUNT() from course, joinedTable
+select course.cnum, COUNT() from course, joinedTable
 inner join class on enrollment.deptcode = class.deptcode, enrollment.cnum = class.cnum, enrollment.term = class.term, enrollment.section = class.section as joinedTable
 
 /*
@@ -110,6 +137,11 @@ more than a single course in any given term. (Note that a percentage
 should be a number between 0 and 100.)
 */
 
+select pnum from class where term as old_term not in
+(
+  select pnum from class where term = old_term
+)
+
 /*
 The number of different third or fourth year students in each section of
 each course taught by a pure math professor in past terms. The result
@@ -126,6 +158,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 c.pnum, c.deptcode, c.cnum, c.term, c.section 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 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