Home  >  Q&A  >  body text

mysql - 如何按照时间段group by

mysql如何分别按照7天,一个季度 来group by 这个时间阶段的count
时间字段存的是regdate

参考百度统计
以下是七天一个跨度

以下是一个季度一个跨度

黄舟黄舟2743 days ago748

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:35:22

    Map time into numbers, use calculated columns, and then group by

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:35:22

    For the quarter, use the month to calculate a new value, and then group by this field
    (month-1)/4+1
    1 2 3 4 ---> 1

    5 6 7 8 ---> 2

    Then, use the week value for 7 days.
    Mysql has a function to get the month or week number of a certain date regdate.

    For example, mongodb has this

    db.sales.aggregate(
       [
         {
           $project:
             {
               year: { $year: "$date" },
               month: { $month: "$date" },
               day: { $dayOfMonth: "$date" },
               hour: { $hour: "$date" },
               minutes: { $minute: "$date" },
               seconds: { $second: "$date" },
               milliseconds: { $millisecond: "$date" },
               dayOfYear: { $dayOfYear: "$date" },
               dayOfWeek: { $dayOfWeek: "$date" },
               week: { $week: "$date" }
             }
         }
       ]
    )

    mongodb’s documentation
    mysql related date functions

    reply
    0
  • Cancelreply