简体   繁体   中英

Get average difference between all unique rows

I have a bantracker which logs to MySQL each ban, I would like to get the average difference between each unix time so I can estimate based on the data when the next ban will happen.

My thinking would be Last entry + average? Maybe I am completely wrong and somebody has a better idea.

The Table structure looks like this: 在此处输入图片说明

Each apikey is unique.

Any help / ideas are much appreciated I don't mind if it's a MySQL query or PHP code.

Thanks!

You're trying to calculate a moving average:

function moving_average( $array ) {

  $z = sizeof( $array );

  for ( $i = 1; $i < $z; $i++ ) {
    $result[] = $array[ $i ] - $array[ $i-1 ];
  }

  return array_sum( $result ) / count( $result );

}

Hat tip to: Calculating the average increase from array values in PHP

You can use(COUNT,SUM) to find average

$sql = "SELECT COUNT(time) as count,SUM(time) as sum,MAX(time) as sum FROM table_name";

$avg=$row["sum"]/$row["count"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM