HIVE- calculate the cumulative and

 

 

 

 

 

 

eg:. Statistics 1-- cumulative sales in December, January is the value that is in January, February and December values ​​in March and 123 in February, in December 1--12 month values ​​and

SELECT  
month,SUM(amount) month_amount,  
SUM( SUM(amount)) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_amount   FROM table_name   GROUP BY month   ORDER BY month;   

Wherein: the interior of SUM (SUM (amount)) SUM (amount) accumulated value required in the above can be replaced month_amount ORDER BY month by month query read recorded sort is the sort within the window

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW define the start and end points, UNBOUNDED PRECEDING as a starting point, indicating that starting from the first row, CURRENT ROW is the default value, which is an equivalent to:

ROWS UNBOUNDED PRECEDING

PRECEDING: N means the front row.

FOLLOWING: means after N lines.

And between 3 months before computing

SUM( SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) AS cumulative_amount 

or

SUM( SUM(amount)) OVER (ORDER BY month 3 PRECENDING) AS cumulative_amount  

And between the front month

SUM( SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS cumulative_amount 

Statistics from 7.23 to 7.26 cumulative and achieve results:

2018-07-23      10527150773     10527150773
2018-07-24      11799445150     22326595923
2018-07-25      11238537810     33565133733
2018-07-26      10917352755     44482486488
select datemion,sum(loan_amount),sum(sum(loan_amount)) OVER (ORDER BY datemion ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) from( select datemion,strdeptcode,loan_amount,b.busiarea_code from a inner b on a.strdeptcode = b.dept_code where a.datemion between '2018-07-23' and '2018-07-26') a group by datemion 

If you follow within a month to accumulate, add partition field to indicate partitioned by month accumulated sum within a month, from the 1st to the end of the month:

select datemion,sum(loan_amount),sum(sum(loan_amount)) OVER (distribute by date_format(datemion,'yyyy-MM') sort BY datemion ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) from( select datemion,strdeptcode,loan_amount,b.busiarea_code from app.app_total_busioverview_aggre a inner join dim.dim_department_v b on a.strdeptcode = b.dept_code where a.datemion between '2018-07-23' and '2018-09-26') a group by datemion order by datemion;

Guess you like

Origin www.cnblogs.com/adolfmc/p/12060837.html