select count(*) as '总人数',(
select count(*) from result r 
where r.studentno = stu.studentno
and
r.subjectid = (select subjectid from subject where subjectName='设计MySchool数据库'
and
r.examdate = (select max(examdate) from result where result.studentno=r.studentno 
and 
result.subjectid = (select subjectid from subject where subjectName='设计MySchool数
据库')
  )
and 
r.studentresult>=60
)
)as '通过人数' 
from student stu
where stu.gradeid = (select gradeid from subject where subjectName='设计MySchool数据库') 帮我看看谢谢

解决方案 »

  1.   

    select count(*) as '总人数',sum(case when b.studentresult>=60 then 1 else 0 end) as '通过人数' 
    from student a
    left join result b on a.studentno=b.studentno  
    left join subject c on b.subjectid=c.subjectid
    where c.subjectName='设计MySchool数据库' 
    and exists(select 1 
       from (select studentno,max(examdate) as examdate from result group by studentno)t 
       where b.studentno=t.studentno and b.examdate=t.examdate
      )