sql >> Base de Datos >  >> RDS >> PostgreSQL

Cómo agrupar por semana en postgresql

Si tiene varios años, también debe tener en cuenta el año. Una forma es:

SELECT date_part('year', author_date::date) as year,
       date_part('week', author_date::date) AS weekly,
       COUNT(author_email)           
FROM commits
GROUP BY year, weekly
ORDER BY year, weekly;

Una forma más natural de escribir esto usa date_trunc() :

SELECT date_trunc('week', author_date::date) AS weekly,
       COUNT(author_email)           
FROM commits
GROUP BY weekly
ORDER BY weekly;