- 论坛徽章:
- 0
|
想做个查询每班考试的平均分优秀率及格率的sql,没想到程序竟然生成了这么长的语句,请问又没有简单的方法作下面查询的?
- select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='一年级一班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='一年级一班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='一年级一班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='一年级二班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='一年级二班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='一年级二班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级一班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级一班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级一班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级三班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级三班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级三班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级二班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级二班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='三年级二班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='二年级一班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='二年级一班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='二年级一班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='二年级二班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='二年级二班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='二年级二班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级一班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级一班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级一班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级三班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级三班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级三班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级二班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级二班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='五年级二班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级一班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级一班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级一班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级三班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级三班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级三班' union select max(class.name), count(*), avg(score), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=90 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级二班') as real)/count(*), cast((select count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.score>=60 and achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级二班') as real)/count(*) from achievement,schoolstu,department,grade,class,exam,course where achievement.schoolstu_id=schoolstu.id and achievement.exam_id=exam.id and schoolstu.class_id=class.id and schoolstu.grade_id=grade.id and schoolstu.department_id=department.id and exam.course_id=course.id and exam.name='2005-2006学年度第二学期第一次月考' and course.name='语文' and class.name='四年级二班'
复制代码
结果:
- max count avg ?column? ?column?
- 一年级一班 18 95.6388888889 0.888888888889 1.0
- 一年级二班 21 87.6904761905 0.857142857143 0.904761904762
- 三年级一班 22 93.6136363636 0.909090909091 1.0
- 三年级三班 22 87.1818181818 0.727272727273 0.954545454545
- 三年级二班 21 95.2380952381 0.952380952381 1.0
- 二年级一班 23 91.9782608696 0.95652173913 0.95652173913
- 二年级二班 22 95.3636363636 1.0 1.0
- 五年级一班 25 87.16 0.24 1.0
- 五年级三班 24 86.4791666667 0.416666666667 1.0
- 五年级二班 25 82.62 0.32 0.96
- 四年级一班 31 87.0161290323 0.677419354839 0.967741935484
- 四年级三班 30 92.1 0.866666666667 0.966666666667
- 四年级二班 31 91.9193548387 0.709677419355 1.0
复制代码 |
|