jenhil34 jenhil34 - 2 years ago 52
SQL Question

SQL Query SUM for each date from first date

I have searched all over and this is driving me crazy. Let's say I have two fields: actiondate and qty. Here is a sample of the data:

actiondate qty
4/22/2016 2
4/23/2016 7
4/23/2016 4
4/25/2016 9
4/26/2016 6
4/26/2016 3
4/26/2016 4

I want to group on the action date, and sum the qty from the beginning date, like this:

actiondate qty
4/22/2016 2
4/23/2016 13
4/25/2016 22
4/26/2016 35

I sincerely appreciate anyone's help on this.

Answer Source

here is quick version:

select actiondate, (
    select sum(qty)
    from table t2
    where t2.actiondate <= t1.actiondate) as qty
from table t1
group by actiondate
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download