有成绩表T_Score(Stu_id和Lession_id为联合主键) 
缺考情况不录入,例如B001的L002课程缺考。 Stu_id(学生号) Lession_id(课程) Score(成绩) 
A001 L001 90 
A001 L002 80 
A002 L001 70 
A002 L002 60 
B001 L001 50 
B002 L001 85 
B001 L002 NULL 
…… …… …… 学生档案T_Stu_Profile(Stu_id为主键) 
包含所有学生信息 Stu_id(学生号) Stu_name(姓名) Class_id(班级) 
A001 张三 06101 
A002 李四 06101 
B001 王五 06102 
…… …… …… 课程信息表T_Lession(Lession_id为主键) 
包含所有课程信息 Lession_id(课程号) Lession_des(课程) 
L001 语文 
L002 数学 
L003 英语 
L004 物理 
L005 化学 1:找出缺考的学生名单,输出如下格式: Class_id(班级) Stu_name(姓名) Lession_des(课程) 
B001 王五 数学 
…… …… …… 
要求:不可以使用游标 
2:输出06101班的学生成绩单,格式如下: 姓名 语文 数学 英语 物理 化学 总分 
…… 
要求:不可以使用游标 3:找出五门课程中的年级前三名,输出如下格式:(假设前三名不出现并列的情况) Lession_des(课程) 第一名 第二名 第三名 
语文 
数学 
英语 
物理 
化学 …… ……

解决方案 »

  1.   

    1:找出缺考的学生名单,输出如下格式:
    ------------------------------------------
    select Class_id,Stu_name,Lession_des from T_Lession a,T_Stu_Profile b,T_Score c
    where a.Lession_id= b.Lession_id and b.Stu_id = c.Stu_id
    and c.Score is null
      

  2.   

    sorry...
    1:找出缺考的学生名单,输出如下格式:
    ------------------------------------------
    select Class_id,Stu_name,Lession_des from T_Lession a,T_Stu_Profile b,T_Scor c
    where a.Lession_id= c.Lession_id and b.Stu_id = c.Stu_id
    and c.Score is null
      

  3.   

    确实蛮麻烦的
    我写了一个游标可以实现第2题 3题的答案与之类似
    create proc mission
    as
    begin
    if exists(select * from sysobjects where name = 'temp' and xtype = 'u') 
    drop table [temp] declare @sqlStr varchar(1000)
    declare @insert varchar(1000) set @sqlStr = 'create table [temp] ( 姓名 varchar(20)'
    declare @LessionName varchar(20)
    declare cur_a cursor
    for
    select Lession_des from T_Lession
    open cur_a
    fetch next from cur_a into @LessionName
    while @@fetch_status = 0
    begin
    set @sqlStr = @sqlStr + ',' + @LessionName + ' int'
    fetch next from cur_a into @LessionName
    end
    close cur_a
    deallocate cur_a
    set @sqlStr = @sqlStr + ',总分 int)'
    print (@sqlStr)
    exec (@sqlStr) declare @Stu_id varchar(20)
    declare @score int
    declare cur_b cursor
    for 
    select sum(T_Scor.Score) as 'Score',T_Stu_Profile.Stu_id
    from T_Scor,T_Stu_Profile --where T_Scor.stu_id = T_Stu_Profile.stu_id
    group by T_Stu_Profile.Stu_id
    having sum(Score) is not null
    open cur_b
    fetch next from cur_b into @score,@Stu_id
    while @@fetch_status = 0
    begin
    set @insert = 'insert into temp values('
    declare @stuName varchar(20)
    select @stuName = Stu_name 
    from T_Stu_Profile
    where Stu_id = @Stu_id
    set @insert = @insert + '''' + @stuName + ''''
    declare @Name varchar(20)
    declare cur_a cursor
    for
    select Lession_des from T_Lession
    open cur_a
    fetch next from cur_a into @Name
    while @@fetch_status = 0
    begin
    declare @ss int
    set @ss = -1
    select @ss = a.Score 
    from T_Scor a,T_Lession b
    where a.Lession_id = b.Lession_id
    and b.Lession_des = @Name
    and a.Stu_id = @Stu_id
    if ( @ss = -1 )
    set @insert = @insert + ',' + 'null'
    else
    set @insert = @insert + ',' + str(@ss)
    fetch next from cur_a into @Name
    end
    close cur_a
    deallocate cur_a
    fetch next from cur_b into @score,@Stu_id
    set @insert = @insert + ',' + str(@score) + ')'
    print @insert
    exec (@insert)

    end
    close cur_b
    deallocate cur_b
    select *from [temp]
    end