SELECT * FROM mydb.现金流;

create view 现金流1 as
select *,
(销售回款流入-开发成本支出-管理费用支出-营销费用支出-财务费用-税金支出) as 项目经营净现金流(不含股东资金成本),
(销售回款流入-开发成本支出-管理费用支出-营销费用支出-财务费用-税金支出+融资放款流入-融资还款支出) as 项目含融资净现金流(不含股东资金成本)
from 现金流;

create view 现金流2 as
select *,
sum(项目经营净现金流(不含股东资金成本)) over(order by 月份 rows between unbounded preceding and current row) as 项目累计经营净现金流(不含股东资金成本),
sum(项目含融资净现金流(不含股东资金成本)) over(order by 月份 rows between unbounded preceding and current row) as 项目累计含融资净现金流(不含股东资金成本)
from 现金流1;

create view 现金流3 as
select *,
(项目累计含融资净现金流(不含股东资金成本)-项目含融资净现金流(不含股东资金成本)/2 )*0.1/12 as 股东资金成本
from 现金流2;

create view 现金流4 as
select 月份,销售回款流入,
row_number() over(order by 月份) 序号,
(经营净现金流(不含股东资金成本)+股东资金成本) as 经营净现金流,
(累计经营净现金流(不含股东资金成本)+股东资金成本) as 累计经营净现金流,
(含融资净现金流(不含股东资金成本)+股东资金成本) as 含融资净现金流,
(累计含融资净现金流(不含股东资金成本)+股东资金成本) as 累计含融资净现金流
from 现金流3;

select * from 现金流4;

输出结果:

现金流表的SQL实现_SQL

值得一提的是用窗口函数实现累加:

sum(字段1) over(partition by 字段2 order by 字段3 rows between unbounded preceding and current row) as 新字段名

除此之外,还可以查找现金流回正月份和周期:

现金流表的SQL实现_现金流表_02

计算净利润:

现金流表的SQL实现_SQL_03