Group rows by 7 days interval starting from a certain date

前端 未结 3 642
不思量自难忘°
不思量自难忘° 2021-01-22 11:01

Is there a way to group rows by a 7 days intervals(datetime) starting from a certain date in Mysql?

3条回答
  •  广开言路
    2021-01-22 11:17

    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 ;
    

提交回复
热议问题