假如数据表是这样的:
2018年1月 100
2018年2月 200
2019年1月 300
2019年2月 400
我想实现:
2018年合计 2019年合计 同比增加
300 700 400
我知道可以用
select sum(a.数据),sum(b.数据), sum(b.数据-a.数据) from 表 as a,表 as b
where a.时间=2018 and b.时间=2019
这类方式来解决但这样需要自连接,数据量较大的时候会很卡
请问能不能做到直接先查2018,再查2019,最后一减换句话说,横着是until,竖着是啥啊?
多谢
2018年1月 100
2018年2月 200
2019年1月 300
2019年2月 400
我想实现:
2018年合计 2019年合计 同比增加
300 700 400
我知道可以用
select sum(a.数据),sum(b.数据), sum(b.数据-a.数据) from 表 as a,表 as b
where a.时间=2018 and b.时间=2019
这类方式来解决但这样需要自连接,数据量较大的时候会很卡
请问能不能做到直接先查2018,再查2019,最后一减换句话说,横着是until,竖着是啥啊?
多谢
(select 1 as amount, 201801 as year
from dual
union all
select 2 as amount, 201802 as year
from dual
union all
select 11 as amount, 201901 as year
from dual
union all
select 2 as amount, 201902 as year
from dual)
select sum(amount) as tot_amount,
lag(sum(amount)) over(order by substr(year, 1, 4)) as last_tot_amount,
sum(amount) - nvl(lag(sum(amount)) over(order by substr(year, 1, 4)), sum(amount)) as minus_amount,
substr(year, 1, 4) as year
from xx
group by substr(year, 1, 4)