Skip to content
Advertisement

How to group by month and year?

I have a table PURCHASE with a date column PURCHASEDATE which is in DATE format. I’m trying to get the purchases grouped by month and year. To do so, I try with:

I have also tryied with GROUP BY EXTRACT(MONTH FROM PURCHASEDATE), EXTRACT(YEAR FROM PURCHASEDATE) but neither worked.

I’m rusty with SQL querys :S.

EDIT

Table cloumns

enter image description here

Thanks ahead!

Advertisement

Answer

If you have the sample data:

Then, you can use your query:

Which outputs:

MONTHYEAR | FREQUENCY
:-------- | --------:
03 2020   |         4
01 2019   |         5
01 2020   |         3
02 2020   |         2
02 2019   |         3
04 2020   |         1

Or, you can use TRUNC:

Which outputs:

MONTHYEAR           | FREQUENCY
:------------------ | --------:
2020-03-01 00:00:00 |         4
2020-04-01 00:00:00 |         1
2020-02-01 00:00:00 |         2
2020-01-01 00:00:00 |         3
2019-01-01 00:00:00 |         5
2019-02-01 00:00:00 |         3

Or, you can use EXTRACT:

Which outputs:

YEAR | MONTH | FREQUENCY
---: | ----: | --------:
2019 |     1 |         5
2020 |     1 |         3
2020 |     2 |         2
2020 |     4 |         1
2019 |     2 |         3
2020 |     3 |         4

db<>fiddle here

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