Moving average - MySQL

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

This question and answers originated from www.stackoverflow.com
Question by (2/17/2011 11:53:26 PM)

Answer

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

Answer by

Find More Answers
Related Topics  mysql  statistics  average  moving-average
Related Questions
  • 10 period moving average in MySql without using date

    I have a table of goalie data, snipet below year gameid player sv% gamenum 2009 200165 John Smith 0.923 0165 2009 209754 John Smith 1.000 9754 2009 206938 John Smith 1.000 6938 2009 206…
  • Moving average/total algorithm

    I need to keep track of the last 7 days work hours in a flat file reading loop. It's being used to measure 'fatigueability' of work rosters. Right now I have something that works, but it seems ra…
  • leading and lagging moving average indicator

    What are leading short and lagging long moving average indicators and how do we calculate them? e.g. for the following data set, and let window size be 2 . Can you show me what the running leading a…
  • Simple Moving Average for stock prices

    I was playing with ActiveQuant FinancialLibrary SimpleMovingAverage function SMA() below: Is there an error in the algo below, where it calculates the average by looking "into the future" ( as it…
  • C# LINQ to calculate a moving average of a SortedList<dateTime,double>

    I have a time series in the form of a SortedList<dateTime,double> . I would like to calculate a moving average of this series. I can do this using simple for loops. I was wondering if there is…
  • Data structure/algorithm to efficiently save weighted moving average

    I'd like to sum up moving averages for a number of different categories when storing log records. Imagine a service that saves web server logs one entry at a time. Let's further imagine, we don't ha…
  • Window moving average in sql server

    I am trying to create a function that computes a windowed moving average in SQLServer 2008 . I am quite new to SQL so I am having a fair bit of difficulty. The data that I am trying to perform the m…
  • MySQL Union and Average

    I've no idea if I am using UNION correctly in this scenario - there may well be a better/easier way and I'm open to suggestions: I have the following code: SELECT COUNT(*), AVG (q1) AS q1, AVG…
  • MySQL Daily Average over a month

    I'm pulling several status over a specific time period and I'd like to pull another stat that would be "average sales per day" over this time period. I'm not sure how to do daily averages over a spe…
  • Getting Average

    How excatly do I calculate the average? Can somebody explain step by step process of how to get an average? If I have an array of numbers (5, 3, 4, 3, 1) and so on. And I need to get the average …