Skip to content
Advertisement

MySQL get sum of amount for last 6 months [closed]

I’m trying to create a query to get the total amount for every month in the last 6 months. The idea is something like:

Sample data

Amount is decimal and date is date time field.

Desired output:

Total month
50.269999504089355 2021 February
17.010000228881836 2021 March

Is it possible to create this with only one query?

Advertisement

Answer

Of course you can. Try this query:

But only month can be duplicate if you consider date range for more than year. january 2020 and january 2021 will be same in above query. So you should group by on month and year as below:

Schema (MySQL v5.7)


Query #1

Output:

Total month
19.239999771118164 2020 December
50.269999504089355 2021 February
35.260000228881836 2021 March

View on DB Fiddle

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement