0

I have a table with a lot of entries pear month from different years, I need to group them by month so after that I could count the entries and make the sum, but I should do it without group the years. The results should be like this:

| year | month | entriesPerMonth   | sumPerMonth   |
|:.....|:......|:..................|:..............|
|2021  |   1   |     ...           |      ...      |  
|2021  |   2   |     ...           |      ...      |  
|2021  |   3   |     ...           |      ...      |  
|2021  |   4   |     ...           |      ...      |  
|2021  |   5   |     ...           |      ...      |  
|2021  |   6   |     ...           |      ...      |  
|2021  |   7   |     ...           |      ...      |  
|2021  |   8   |     ...           |      ...      |  
|2021  |   9   |     ...           |      ...      |   
|2021  |   10  |     ...           |      ...      | 
|2021  |   11  |     ...           |      ...      |
|2021  |   12  |     ...           |      ...      | 
|2020  |   1   |     ...           |      ...      |   
|2020  |   2   |     ...           |      ...      |  
|2020  |   3   |     ...           |      ...      |  
|2020  |   4   |     ...           |      ...      |  
|2020  |   5   |     ...           |      ...      |  
|2020  |   6   |     ...           |      ...      |  
 ...      ...        ...                  ...
|2019  |   1   |     ...           |      ...      | 
|2019  |   2   |     ...           |      ...      | 
|2019  |   3   |     ...           |      ...      |  
|2019  |   4   |     ...           |      ...      | 
// and so on ...

The thing is that I don't know how to "say" in the query: "Only group month if the year are the same", or something like that. Thanks to anyone ho take the time.

Kleber Germano
  • 702
  • 3
  • 10
  • 25

1 Answers1

1

You can try below query using YEAR() and MONTH() functions -

SELECT YEAR(DATE_COLUMN) `YEAR`, MONTH(DATE_COLUMN) `MONTH`, <AGGREGATE COLUMNS>...
  FROM YOUR_TABLE
 GROUP BY YEAR(DATE_COLUMN), MONTH(DATE_COLUMN)
Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
  • I don't believe was a thing so simple, I've been trying to 'solve' this for 2 days, with CASE clauses among other things... really thanks a lot Ankit Bajpai – Kleber Germano May 03 '21 at 14:52