SQL 一条SQL语句 统计 各班总人数,男女各总人数 ,各自 男女 比例

/* 

*

*

select  name , count(*) num from test where name='男' group by name */

统计相等的数量

result    name  num

             男        2

 

select  sClass 班级,count(*)  班级学生总人数,
sum(case when sGender=0 then 1 else 0 end) 女生人数,
sum(case when sGender=0 then 1 else 0 end)*1.0/count(*)女生所占比例,
sum(case when sGender=1 then 1 else 0 end) 男生人数,
sum(case when sGender=1 then 1 else 0 end)*1.0 /count(*)男生所占比例  
from student GROUP BY sClass ORDER BY sClass asc

 

declare @Major nvarchar(20)
declare @KSH nvarchar(20)
set @KSH='123'  -- 23456789012345 12345678901234
set @Major = (select Major from JQ_Examinee_Info_Manage where KSH=@KSH);
 
select Class,count(Class) 班级人数,
    sum(case when Examinee_Sex='男' then 1 else 0 end ) 男生人数,
    sum(case when Examinee_Sex='女' then 1 else 0 end ) 女生人数 
    from (SELECT Class,Examinee_Sex from JQ_Examinee_Info_Manage where Major=@Major) as t
 group by t.Class;

--------------------- 
作者:Violent_clown 
来源:CSDN 
原文:https://blog.csdn.net/Violent_clown/article/details/81707070 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(DataBase)