MySQL 'group by' together with 'partition by'

Online MySQL Database

create table Test(cc varchar(25), item varchar(100), num integer);
insert into Test(cc, item, num) values('ca', '8.1.4', 11);
insert into Test(cc, item, num) values('cn', '8.1.1', 1);
insert into Test(cc, item, num) values('cn', '8.1.4', 15);
insert into Test(cc, item, num) values('cn', '8.1.3', 5);
insert into Test(cc, item, num) values('cn', '8.1.1', 2);
insert into Test(cc, item, num) values('en', '8.1.4', 12);
insert into Test(cc, item, num) values('eu', '8.1.4', 10);
insert into Test(cc, item, num) values('hk', '8.1.1', 2);
insert into Test(cc, item, num) values('jp', '8.1.3', 6);
insert into Test(cc, item, num) values('us', '8.1.1', 3);
insert into Test(cc, item, num) values('us', '8.1.4', 10);

/* Your code here! */
select a.cc,a.item, sum(a.num), min(a.num), min(sum(a.num)) over (partition by a.cc) as amount_sum_min,
sum(min(a.num)) over (partition by a.cc) as amount_min_sum
from Test a
group by a.cc, a.item
order by a.cc, a.item;

cc item sum(a.num) min(a.num) amount amount_2
ca 8.1.4 11 11 11 11
cn 8.1.1 3 1 3 21
cn 8.1.3 5 5 3 21
cn 8.1.4 15 15 3 21
en 8.1.4 12 12 12 12
eu 8.1.4 10 10 10 10
hk 8.1.1 2 2 2 2
jp 8.1.3 6 6 6 6
us 8.1.1 3 3 3 13
us 8.1.4 10 10 3 13

Note the difference between columns 'amount_sum_min' and 'amount_min_sum'. The SQL first executes the group_by statement, and then based on the group_by result, apply analytical function onto it. Understanding this, it'll be easier for you to generate monthly running sum from daily revenue $ using SQL like below:

select sum(sum(revenue)) over (partition by month order by month) as monthly_running_sum
from your_db
group by month
order by month;