How to group data per week in mysql

Asked

Viewed 199 times

1

I am extracting some data and want to know some way to group the data per week. I am giving the SELECT and he’s bringing in last month’s data and I’m already grouping by day, but I need it by week.

  • 1

    can post your query to get an idea of your tables and columns.

  • Check this link: https://www.w3resource.com/mysql/date-and-time-functions/mysql-week-function.php Basically using the week operator based on a date Voce gets the week number

1 answer

1

I decided by giving the GROUP BY YEARWEEK(DATA,1) - 1 is the first day of the week.

SELECT DISTINCT DATE_FORMAT(created_at, '%Y-%m-%d') AS 'Primeiro dia da semana', COUNT(id) AS 'Total'
FROM payments 
WHERE status = 2 AND (created_at BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 month) AND CURDATE())
GROUP BY YEARWEEK(created_at, 1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.