CREATE TABLE t_student (
f_id char(3)   NOT NULL Primary Key,
f_name char(8)   NOT NULL ,
f_sex char(1)   NOT NULL ,
f_birth date,
f_department char(5),
f_class int
);CREATE TABLE t_course(
f_id char(2) NOT NULL Primary Key,
f_name char(10),
f_teacher char(8)
);CREATE TABLE t_grade (
f_stuid char(3) NOT NULL REFERENCES t_student(f_id),
f_courseid char(2) NOT NULL REFERENCES t_course(f_id),
f_grade int NULL ,
Primary Key(f_stuid,f_courseid)
);1.统计各门课程的总人数、及格人数和不及格人数(课程编号、课程名、总人数、及格人数、不及格人数)
2.查询未选课程的学生名单(学号、姓名),查询选择所有课程的学生名单
??????????????????、、分不多了。请教各位大侠!

解决方案 »

  1.   

    --    1.统计各门课程的总人数、及格人数和不及格人数(课程编号、课程名、总人数、及格人数、不及格人数)
    select 
        b.f_id 课程编号 ,
        b.f_name 课程名 ,
        sum(1) 总人数     ,
        sum(case when f_grade >= 60 then 1 else 0) 及格人数 ,
        sum(case when f_grade >= 60 then 0 else 1) 不及格人数
    from 
        t_grade a,
        t_course b
    where 
        a.f_courseid=b.f_id
    group by
        b.f_id,
        b.f_name;--    2.查询未选课程的学生名单(学号、姓名),查询选择所有课程的学生名单--    查询未选课程的学生名单(学号、姓名),
    select   
       a.f_id 学号 ,
       a.f_name 姓名
    from
       t_student a
    where 
       not exsit ( select 1 from t_grade b where a.f_id = b.f_stuid) ;
     
    --    查询选择所有课程的学生名单 
    select                                                          
        c.f_id 学号 ,                                                
        c.f_name 姓名                                                
    from                                                            
        select b.f_id ,b.f_name,count(1) coure_num1 from t_grade a,t_student b  
        where a.f_stuid=b.f_id grup by b.f_id,b.f_name) c
        t_grade a,t_student b,(select count(1) coure_num2 from  t_course ) d                                               
    where                                                           
        a.coure_num1=b.coure_num2;
      

  2.   

    sum(case when f_grade >= 60 then 1 else 0)
    缺少关键字,我用的Oracle9i
      

  3.   


    不好意思 缺少 end了 --    1.统计各门课程的总人数、及格人数和不及格人数(课程编号、课程名、总人数、及格人数、不及格人数)
    select 
        b.f_id 课程编号 ,
        b.f_name 课程名 ,
        sum(1) 总人数     ,
        sum(case when f_grade >= 60 then 1 else 0 end) 及格人数 ,
        sum(case when f_grade >= 60 then 0 else 1 end) 不及格人数
    from 
        t_grade a,
        t_course b
    where 
        a.f_courseid=b.f_id
    group by
        b.f_id,
        b.f_name;
      

  4.   


    --1、
    select  
         b.f_id 课程编号 ,
         b.f_name 课程名 ,
         sum(1) 总人数 ,
         sum(case when f_grade >= 60 then 1 else 0) 及格人数 ,
         sum(case when f_grade >= 60 then 0 else 1) 不及格人数
    from t_grade a,t_course b
    where a.f_courseid=b.f_id
    group by b.f_id,b.f_name;-- 2、
    -- 未选课程
    select a.f_id 学号,a.f_name 姓名
    from t_student a
    where not exsits(select 1 from t_grade where a.f_id =f_stuid) ;
     
    -- 选择所有课程的 
    select a.f_id 学号,a.f_name 姓名   
    from t_student a,(select count(1) num1 from t_course ) b, 
       (select b.f_id ,b.f_name,count(1) num2 
        from t_grade a,t_student b   
        where a.f_stuid=b.f_id group by b.f_id,b.f_name) c  
    where a.f_id=c.f_id and b.num1=c.num2;