Latest

Thursday, July 6, 2017

Laravel how to get the AVG value of first 3 records from each group

Asked by: Eranga Kapukotuwa


I have a database table, which includes points against the player. A Single player have multiple records in the table. I need to group the records by the player_id. And I am trying to get the average of first 3 records of the each group. I tried to reach this over the last evening. But I had not any luck to solve this.

This is my initial code, And I need to modify this to get the AVG of first 3 records of each group(grouped by player_id)

$oSelect = DB::table('statistics')
          ->where('statistics.league_id', $league_id)
          ->select('id, AVG('statistics.points as points'),player_id, scheduled')
          ->orderBy('statistics.scheduled','DESC')
          ->groupBy('statistics.player_id');

Thank you in advanced



Source

No comments:

Post a Comment

Adbox