Let’s suppose that I have the following data:
CustomerID->Number->Date 1->2->1/1/2019, 1->3->2/1/2019, 2->1->1/1/2019, 2->6->2/1/2019
I want to have a column let’s call it Summation
:
CustomerID->Number->Date->Summation 1->2->1/1/2019->2, 1->3->2/1/2019->5, 2->1->1/1/2019->1, 2->6->2/1/2019->7
I want that column Summation
in each row to represent the total summation of customer from the first of the month until the day each row has.
How to do this in Python, SQL, or R?
Advertisement
Answer
In SQL Server
SELECT CustomerID,Number, DATE, SUM(Number) over (partition by CustomerID order by Date) as summation from tab_name