Sí, es posible. En este caso, primero debes ajustar el formato del campo de fecha y luego modificar la agrupación. A continuación se muestra la consulta ajustada.
-- [params]
-- date :start_date
-- date :end_date
WITH data AS
(SELECT
date_part('month', ar.date) AS month,
date_part('year', ar.date) AS year,
CASE
WHEN ar.req_type=6 THEN 'Crawlers'
WHEN ar.req_type=7 THEN 'Logged in users'
WHEN ar.req_type=8 THEN 'Anonymous users'
END AS pageview,
ar.count AS views
FROM application_requests ar
WHERE req_type IN (6,7,8)
AND ar.date::date BETWEEN :start_date::date
AND :end_date::date
ORDER BY ar.date ASC, ar.req_type
)
SELECT
month,
year,
sum(views) AS qtt_views
FROM data
GROUP BY year, month
ORDER BY year DESC, month ASC
| month | year | count |
|---|---|---|
| 1 | 2021 | 1000 |
| 2 | 2021 | 500 |
| 1 | 2020 | 1500 |
| 2 | 2020 | 2000 |
| 3 | 2020 | 2500 |