SQL⼀条SQL语句统计各班总⼈数、男⼥各总⼈数、该班级男⼥
⽐例
1select count(*) as numOfTotal,
2sum(case when t.[gender]='male'then1else0end) as numOfMale , --男⽣⼈数
3sum(case when t.[gender]='female'then1else0end) numOfFemale, --⼥⽣⼈数
4sum(case when t.[gender]='male'then1else0end)*1.0/count(*) as percentOfMale,
5sum(case when t.[gender]='female'then1else0end)*1.0/count(*) as perscentOfFemale
6from
7 (SELECT a.[userid],a.[module],a.[amount],b.[name],b.[gender]
8FROM[PracticeSQL].[dbo].[module] a inner join[PracticeSQL].[dbo].[user] b
9on a.[userid]=b.[userid]) t
sql统计每个系的学生人数输出结果:
numOfTotal numOfMale numOfFemale percentOfMale perscentOfFemale
18 14 4 0.777777777777 0.222222222222
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论