![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
统计表中一列中各类型的人数而且每一个学院的统计结果用一行显示
学院 总人数 毕业人数 结业人数
人文学院 300 298 2
计算机 320 299 1
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
.
sql语句:
select jw_xy, count(jw_xy) as 总人数,sum(case when jw_bzh='毕业' then 1 else 0 end) as 毕业人数,sum(case when jw_bzh='结业' then 1 else 0 end) as 结业人数 from jwc_GraduationAudit group by jw_xy
---------------------------------
求一列各种情况之和(每一种结果以行显示)
create view V_xscj_kcjbxx
as
select xh, kcxz,xscj.kcxf from xscj,kcjbxx where xscj.kcbh=kcjbxx.kcbh
select xh,kcxz, sum(kcxf) as total from V_xscj_kcjbxx group by xh,kcxz order by xh
显示结果:
xh kcxz total
040121001 选修课 3.0
040121001 必修课 179.5
040121001 任意选修课 5.0
040121001 限定选修课 1.5
----------------------------------
select xh,(select sum(kcxf) where kcxz='选修课' ) as 选修课 , (select sum(kcxf) where kcxz='必修课') as 必修课,(select sum(kcxf) where kcxz='任意选修课') as 任意选修课,(select sum(kcxf) where kcxz='限定选修课') as 限定选修课
from V_xscj_kcjbxx group by xh,kcxf order by xh
显示结果:
xh 选修课 限定选修课 必修课 任意选修课 040121001 3.0 NULL NULL NULL 040121001 NULL NULL NULL 179.5 040121001 NULL NULL 5.0 NULL 040121001 NULL 1.5 NULL NULL
----------------------------
求一列各种情况之和(每一种结果以列显示)
select xh,
sum(case kcxz when '选修课' then kcxf end) as 选修课,
sum(case kcxz when '必修课' then kcxf end) as 必修课,
sum(case kcxz when '任意选修课' then kcxf end) as 任意选修课,
sum(case kcxz when '限定选修课' then kcxf end) as 限定选修课
from V_xscj_kcjbxx group by xh order by xh
结果显示:
xh 选修课 限定选修课 必修课 任意选课 040121001 3.0 5.0 179.5 1.0 179.5 -----------------------------------------------------------------------
统计各个学院年龄段人数
SELECT bmmc, CASE WHEN DATEDIFF(YY, cssj, GETDATE())
< 20 THEN '0-19岁' WHEN DATEDIFF(YY, cssj, GETDATE()) BETWEEN 20 AND
29 THEN '20-29岁' WHEN DATEDIFF(YY, cssj, GETDATE()) BETWEEN 30 AND
39 THEN '30-39岁' WHEN DATEDIFF(YY, cssj, GETDATE()) BETWEEN 40 AND
50 THEN '40-50岁' ELSE '50岁以上' END AS age
FROM jsjbxx
ORDER BY age