求同两张表4个结果集的横向连接
表1   学生表(学号   班级   是否及格) 
 表2   奖惩表(班级   奖惩) 
 其中是否及格和奖惩都是用0,1两种状态表示 
 现在要求出以下结果 
 总人数   及格数   奖励数   惩罚数   班级   
 请问如何实现
------解决方案--------------------  --创建测试环境 
 create table #学生表(学号 int, 班级 int, 是否及格 int)   
 create table  #奖惩表(班级 int, 奖惩 int)   
 --查询语句 
 select  
      count(*) as 总人数, 
      sum(case when a.是否及格=1 then 1 else 0 end) as 及格数, 
      sum(case when b.奖惩=1 then 1 else 0 end) as 奖励数, 
      sum(case when b.奖惩=0 then 1 else 0 end) as 奖励数, 
      班级 
 from #学生表 as a 
     inner join #奖惩表 as b on a.班级=b.班级 
 group by 班级     
 --清理测试环境 
 drop table #学生表,#奖惩表
------解决方案--------------------select  
 count(*) 总人数 ,  
 isnull(sum(case when 是否及格=1 then 1 end ),0) 及格数, 
 isnull(sum(case when 奖惩=1 then 1 end),0) 奖励数, 
 isnull(sum(case when 奖惩=0 then 1 end ),0) 惩罚数,  
 学生表.班级  
 from 学生表,奖惩表  
 where  学生表.班级=奖惩表.班级  
 group by 学生表.班级
------解决方案--------------------表1 学生表(学号 班级 是否及格) 
 表2 奖惩表(班级 奖惩) 
 其中是否及格和奖惩都是用0,1两种状态表示 
 现在要求出以下结果 
 总人数 及格数 奖励数 惩罚数 班级   
 select a.总人数,a.及格数, b.奖励数,b.惩罚数,a班级 from  
 (select  班级,count(*) as 总人数, count(case when 是否及格=1 then 1 else 0 end ) as 及格数 from 表1 group by 班级) a 
 left join  
 (select  班级,count(case when 奖励数=1 then 1 else 0 end ) as 奖励数, count(case when 奖励数=0 then 1 else 0 end ) as 惩罚数,from 表2 group by 班级) b  
 on a.班级=b.班级
------解决方案--------------------select a.总人数,a.及格数, b.奖励数,b.惩罚数,a班级 from  
 (select  班级,count(*) as 总人数, sum(case when 是否及格=1 then 1 else 0 end ) as 及格数 from 表1 group by 班级) a 
 left join  
 (select  班级,sum(case when 奖励数=1 then 1 else 0 end ) as 奖励数, sum(case when 奖励数=0 then 1 else 0 end ) as 惩罚数,from 表2 group by 班级) b  
 on a.班级=b.班级