Moving average - MySQL

◇◆丶佛笑我妖孽 提交于 2019-12-07 14:45:40

问题


I'm trying to implement system-wide login throttling and I need to calculate the daily average number of failed login attempts from the last 3 months.

I'm currently inserting a record on every login fail, each with a timestamp. How can I do this in MySQL?

Thanks in advance for your help


回答1:


SELECT AVG(cnt)
  FROM (SELECT COUNT(*) AS cnt
          FROM mytable
         WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 3 MONTH) AND NOW()
      GROUP BY DATE(`date`)) x

Assuming you have a table mytable with field date of type date, datetime or timestamp



来源:https://stackoverflow.com/questions/5036176/moving-average-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!