Forum Stats

  • 3,824,920 Users
  • 2,260,440 Discussions
  • 7,896,347 Comments

Discussions

MySQl Count aggregation until last month vs current month

User_MS2CU
User_MS2CU Member Posts: 40 Red Ribbon
edited May 8, 2022 10:21AM in MySQL Community Space


Hello,

I'm attaching a sample data by year/month/quantity. What I need to show is "Quantity Change". The calculation that need to do is:

"Quantity Change" = sum(quantity) until last month - sum(quantity) until current month

Here is what I need to show if I'm running this query today (current month is 5):

Quantity_Change

8

if current month is 4 then:

Quantity_Change

21

and so on... I've added the expected "Quantity_Change" column with some helpful columns to do the calculation. btw, need to find the current system date now() or current_date to find the previous month.


Any help and support is really appreciated.

Thanks