Group rows by 7 days interval starting from a certain date

前端 未结 3 640
不思量自难忘°
不思量自难忘° 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 ;
    
    0 讨论(0)
  • 2021-01-22 11:20
    SELECT users.* from users 
    WHERE created_at >= '2011-12-01' 
    AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
    

    This selects the users created between 201-12-01 and 7 days after that. Make changes, to query based on your need.

    0 讨论(0)
  • 2021-01-22 11:24
    SELECT *
    FROM  `table` 
    GROUP BY WEEK( ADDDATE( `date_column` , WEEKDAY(NOW()) ) ) 
    
    0 讨论(0)
提交回复
热议问题