Mysql
 sql >> Database >  >> RDS >> Mysql

Raggruppa le righe per intervalli di 7 giorni a partire da una determinata data

SELECT 
    1 + DATEDIFF(columnDate, @start_date) DIV 7  AS weekNumber
  , @start_date + INTERVAL (DATEDIFF(columnDate, @start_date) DIV 7) WEEK
      AS week_start_date
  , MIN(columnDate) AS actual_first_date
  , MAX(columnDate) AS actual_last_date
  , SUM(otherColumn)
  , AVG(otherColumn)
  --- 
FROM 
    tableX 
WHERE 
    columnDate >= @start_date 
GROUP BY
    DATEDIFF(columnDate, @start_date) DIV 7 ;