Oracle 简单统计示例
有数据如下:
eg1:现在需要统计所有男性人员数量,所有女性人员数量,sclassno=10000的男性人员的总年龄,年龄大于20的女性人员数量
----sign( number )
/*If number < 0, then sign returns -1.
If number = 0, then sign returns 0.
If number > 0, then sign returns 1*/
select count(decode(t.sgender, 'M', 1, null)) "男性人员数量", count(decode(t.sgender, 'F', 1, null)) "女性人员数量", sum(decode(t.sclassno, '10000', decode(t.sgender, 'M', t.sage, 0), 0)) "10000的男性人员的总年龄", count(decode(t.sgender, 'F', decode(sign(t.sage - 20), 1, 1, null), null)) ">20女性数量", --另一种写法 count(decode(t.sgender,'F',(case when t.sage > 20 then 1 else null end),null)) ">20女性数量" from l_student_info_tbl t;
eg2:统计每个班级人员的男女数量,总年龄
select t.sclassno "班级", count(decode(t.sgender, 'M', 1, null)) "男性人员数量", count(decode(t.sgender, 'F', 1, null)) "女性人员数量", sum(decode(t.sgender, 'M', t.sage, 0)) "男性总年龄", sum(decode(t.sgender, 'F', t.sage, 0)) "女性总年龄" from l_student_info_tbl t group by t.sclassno