declare @username nvarchar(20)
set @username='...'
select
a.title,
isnull(sum(c.u_score),0) 实际得分,
isnull(sum(c.m_score),0) 应得总分
from
(select * from classlist where parent=0) a
inner join
(select * from classlist where parent!=0) b
on
a.id=b.parent
left join
(select class,
(select max(score) from answer where stid=news.id) m_score,
(select s.score from grade g,answer s where g.stid=s.stid and g.anid=s.id and g.stid=news.id and g.username=@username) u_score
from news)c
on
b.id=c.class
group by
a.id,a.title
order by
a.id