方法二 详解
将月总金额表 自己连接 自己连接
select A.*,B.* FROM (select username,month,sum(salary) as salary from t_salary_detail group by username,month) A inner join (select username,month,sum(salary) as salary from t_salary_detail group by username,month) B ons A.username=B.username where B.month <= A.month +-------------+----------+-----------+-------------+----------+-----------+--+ | a.username | a.month | a.salary | b.username | b.month | b.salary | +-------------+----------+-----------+-------------+----------+-----------+--+ | A | 2015-01 | 33 | A | 2015-01 | 33 | | A | 2015-02 | 10 | A | 2015-01 | 33 | | A | 2015-02 | 10 | A | 2015-02 | 10 | | A | 2015-03 | 16 | A | 2015-01 | 33 | | A | 2015-03 | 16 | A | 2015-02 | 10 | | A | 2015-03 | 16 | A | 2015-03 | 16 | | B | 2015-01 | 30 | B | 2015-01 | 30 | | B | 2015-02 | 15 | B | 2015-01 | 30 | | B | 2015-02 | 15 | B | 2015-02 | 15 | | B | 2015-03 | 17 | B | 2015-01 | 30 | | B | 2015-03 | 17 | B | 2015-02 | 15 | | B | 2015-03 | 17 | B | 2015-03 | 17 | +-------------+----------+-----------+-------------+----------+-----------+--+从上一步的结果中 进行分组查询,分组的字段是a.username a.month 求月累计值: 将b.month <= a.month的所有b.salary求和即可
select A.username,A.month,max(A.salary) as salary,sum(B.salary) as accumulate from (select username,month,sum(salary) as salary from t_salary_detail group by username,month) A inner join (select username,month,sum(salary) as salary from t_salary_detail group by username,month) B on A.username=B.username where B.month <= A.month group by A.username,A.month order by A.username,A.month; +-------------+----------+---------+-------------+--+ | a.username | a.month | salary | accumulate | +-------------+----------+---------+-------------+--+ | A | 2015-01 | 33 | 33 | | A | 2015-02 | 10 | 43 | | A | 2015-03 | 16 | 59 | | B | 2015-01 | 30 | 30 | | B | 2015-02 | 15 | 45 | | B | 2015-03 | 17 | 62 | +-------------+----------+---------+-------------+--+绝对路径
select step, numbs, first_value(numbs) over (order by step) numb2, numbs/ first_value(numbs) over (order by step)*100 absolutePath from dw_oute_numbs;相对路径
select step, numbs, LAG(numbs,1) over(order by step) numb2, numbs/(lag(numbs,1) over (order by step))*100 relativePath from dw_oute_numbs;