-1
Here is a suggestion to test using the Row_number function to number the lines per month and in descending order of quantity:
with CTE_RN as
(
select
mes,
rubrica,
count(*) qtd,
row_number() over(partition by mes order by count(*) desc) as RN
from boletins
group by
mes,
rubrica
)
select mes, rubrica, qtd
from CTE_RN
where RN <= 3
order by mes asc, qtd desc
I hope it helps
add
limit 3
at the end of the consultation– Pedro Sanção