mysql按周分组,并在每周总计

时间:2018-07-25 14:16:38

标签: mysql

我正在尝试创建一个查询,该查询将基于每周返回一个数字的总数,以便创建上升趋势折线图。在我的表格中,我有很多记录完成日期(完成)的记录。我希望能够创建一个查询,该查询每周生成一次滚动总计。因此,如果第1周有10个完成,第2周有15个完成,第3周有5个完成,则所需结果将是:

第1周总计:10 第二周总计:25 第三周总计:30

样本数据:

 id status  sched
 12 Successful  2017-04-04 00:00:00.000
 15 Successful  2017-06-20 19:30:00.000
 18 Successful  2017-10-17 18:00:00.000
 26 Successful  2017-04-05 00:00:00.000
 29 Successful  2017-06-16 00:00:00.000
 30 Successful  2017-04-06 00:00:00.000
 31 Successful  2017-04-07 00:00:00.000
 32 Successful  2017-04-06 00:00:00.000
 34 Successful  2017-10-18 18:00:00.000
 35 Successful  2017-06-13 00:00:00.000

这是我用来按周成功生成数据而没有任何汇总的查询。我尝试添加“ WITH ROLLUP”,但最后只给出了总计,而不是每周。

select DATE_FORMAT(completed,'%d/%m/%Y') AS nd , wk, count(*)as totals
from
(
   select id, completed, yearweek(completed)as wk from w10_upgrades
   where status = 'Successful' and type = 'Normal'
   and yearweek(completed) is not null
) x
GROUP BY wk
ORDER BY wk;

所需的输出:

 wk     totals
 201714   10
 201715   25 (output would = week 201714 + 201715)
 201716   55 (output would = week 201714 + 201715 + 201716)
 ect...

任何方向都值得赞赏。我找不到与此相关的任何东西。

1 个答案:

答案 0 :(得分:0)

最终结果

With Cm
    .ActiveConnection = Appconn
    .CommandType = adCmdText
    For Each x In myarray
        x = "'" & x & "'"
    Next
    .CommandText = "UPDATE [Attendance] SET [Seated] = '1' WHERE [Agentname] In ('" & VBA.Join(myarray, "','") & "')"
    Set AttendRecord = .Execute
End With