时间:2021-07-01 10:21:17 帮助过:5人阅读
select DW_NAME, COUNT(DW_NAME) as 人数 from TEST_TEACHER group by DW_NAME order by DW_NAME;输出结果如下图所示:其中order by是按汉语拼音排序输出。
select DW_NAME, COUNT(DW_NAME) as SUM, COUNT(CASE WHEN SEX='男' THEN 1 END) as Man, COUNT(CASE WHEN SEX='女' THEN 1 END) as Women, COUNT(CASE WHEN DEGREE='博士' THEN 1 END) as BS, COUNT(CASE WHEN DEGREE='硕士' THEN 1 END) as SS from TEST_TEACHER group by DW_NAME order by DW_NAME;输出结果如下图所示:表示如何SEX为"男",统计加1。
select DW_NAME, COUNT(DW_NAME) as SUM, ratio_to_report(COUNT(1)) OVER(), COUNT(CASE WHEN SEX='男' THEN 1 END) as Man, COUNT(CASE WHEN SEX='女' THEN 1 END) as Women from TEST_TEACHER group by DW_NAME order by DW_NAME;输出结果如下图所示:其中计算机3个老师,总数10个老师,比例占0.3。
select t2.DW_NAME, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME) as 总数, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') as 男, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='女') as 女 from TEST_DEP t2;输出结果如下图所示:这种方法是非常常见的一种统计方法,而不是仅仅通过单表,因为数据库往往都会通过外键联系其他表。
select t2.DW_NAME, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME) as 总数, ((select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME) / (select COUNT(*) from TEST_TEACHER)) as 总数比例, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') as 男, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='女') as 女, ((select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') / (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME)) as 男教师比例 from TEST_DEP t2;输出结果如下图所示:
select t2.DW_NAME, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME) as 总数, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') as 男, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='女') as 女, trunc(((select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') / (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME))*100,2) as 百分比 from TEST_DEP t2;输出结果如下图所示:
select t2.DW_NAME, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME) as 总数, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') as 男, (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='女') as 女, trunc(decode((select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME), 0, 0, ((select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME and SEX='男') / (select COUNT(*) from TEST_TEACHER t1 where t1.DW_NAME=t2.DW_NAME)))*100,2) as 百分比 from TEST_DEP t2;输出结果如下图所示,核心方法:trunc(decode(b,0,0,a/b)*100, 2)。
select t1.DW_NAME, t2.ZS, t3.BS, t4.DW_NAME, t5.ZS, t6.BS from (select DW_NAME as DW_NAME from TEST_TEACHER where DW_NAME='软件学院' group by DW_NAME) t1, (select COUNT(*) as ZS from TEST_TEACHER where DW_NAME='软件学院') t2, (select COUNT(*) as BS from TEST_TEACHER where DW_NAME='软件学院' and DEGREE='博士') t3, (select DW_NAME as DW_NAME from TEST_TEACHER where DW_NAME='计算机学院' group by DW_NAME) t4, (select COUNT(*) as ZS from TEST_TEACHER where DW_NAME='计算机学院') t5, (select COUNT(*) as BS from TEST_TEACHER where DW_NAME='计算机学院' and DEGREE='博士') t6;纵向连接:使用UNION ALL连接
select DW_NAME, COUNT(DW_NAME) as SUM, ratio_to_report(COUNT(1)) OVER() as BL, COUNT(CASE WHEN SEX='男' THEN 1 END) as Man, COUNT(CASE WHEN SEX='女' THEN 1 END) as Women from TEST_TEACHER WHERE DW_NAME='软件学院' GROUP BY DW_NAME UNION ALL select DW_NAME, COUNT(DW_NAME) as SUM, ratio_to_report(COUNT(1)) OVER() as BL, COUNT(CASE WHEN SEX='男' THEN 1 END) as Man, COUNT(CASE WHEN SEX='女' THEN 1 END) as Women from TEST_TEACHER WHERE DW_NAME='计算机学院' GROUP BY DW_NAME;输出结果如下图所示:
[数据库] Oracle单表查询总数及百分比和数据横向纵向连接
标签: