废话很少说,咱们直接进行测试。测试
咱们通常的写法就是使用group by进行分组。3d
select t.ssex, count(t.sname) from STUDENT t group by t.ssex;
若是咱们须要求男生的人数是女生的多少倍怎么计算?code
select "男生人数", "女生人数", "男生人数" / "女生人数" as 倍数 from (select sum(case when t.ssex = '男' then 1 else 0 end) as "男生人数", sum(case when t.ssex = '男' then 1 else 0 end) as "女生人数" from STUDENT t)
通俗理解:就是将group by t.ssex的结果进行”行转列”操做
blog
同比通常状况下是今年第n月与去年第n月比。
公式:同比增加率=(本期数-同期数)÷同期数×100%
通常理解:同比增加率=(今年第n月的数据-去年第n月数据)÷去年第n月数据×100%get
select "本期收入", "同期收入", to_char(("本期收入" - "同期收入") / "同期收入",'fm99990.00') "同比" from (select sum(case when t.year = 2019 and month = 5 then t.money else 0 end) as "本期收入", sum(case when t.year = 2018 and month = 5 then t.money else 0 end) as "同期收入" from income t where t.name = '张三')
环比,表示连续2个单位周期(好比连续两月)内的量的变化比。
公式:环比增加率=(本期数-上期数)/上期数×100%select
2019年5月收入环比im
select "本期收入", "上期收入", to_char(("本期收入" - "上期收入") / "上期收入",'fm99990.00') "环比" from (select sum(case when t.year = 2019 and month = 5 then t.money else 0 end) as "本期收入", sum(case when t.year = 2019 and month = 4 then t.money else 0 end) as "上期收入" from income t where t.name = '张三')