SELECT class AS 班级 , SUM(Amount) AS 考试分数 , COUNT(DISTINCT UserName) AS 考试人数 , COUNT(Name) AS 语文分数 FROM dbo.[t-A] GROUP BY Class , Name HAVING Name = '语文'
好像有点不对
SELECT A.class AS 班级 ,SUM(Amount) AS 总分 ,COUNT(DISTINCT UserName) AS 考试总人数 ,quantity AS 语文参考人数 FROM A left join (select class,count(username) AS quantity from A where name='语文' group by class) AS B on A.class=B.class group by A.class,quantity order by A.class desc
赞同楼上
楼主要的不是语文总成绩 是语文参加考试的人数
if object_id('tempdb..#a') is not null drop table #a go create table #a ( id int , Class varchar(20), UserName varchar(20), Name varchar(20), Amount int ) insert into #a select 1 ,'一班','张三','数学',90 union all select 2 ,'一班','张三','语文',89 union all select 3 ,'一班','李四','数学',26 union all select 4 ,'一班','李四','语文',31 union all select 5 ,'二班','王五','数学',90 union all select 6 ,'二班','王五','语文',80 union all select 7 ,'二班','钟伟','数学',70 select Class as 班级, sum(Amount) as 总分, count( DISTINCT UserName) as 考试总人数, (select count(UserName) from #a b where b.Class=a.class and b.Name='语文' )as 语文参考人数 from #a a group by class order by 班级 desc /*****************************/ 班级 总分 考试总人数 语文参考人数 一班 236 2 2 二班 240 2 1
select Class as 班级, sum(Amount) as 总分,
count( DISTINCT UserName) as 考试总人数,
count(distinct(case when Name='语文' then UserName else null end)) as 语文考试人数
from tableName a
group by class
order by 班级 desc