Just to make sure I understand. You want the count of the marks by student_id and paper_id? However, only count those marks that are greater than the grade boundary?
How to determine if multiple records' are more than a value then count?
I have a results table containing:
$table->increments('id');
$table->integer('mark')->nullable();
$table->integer('paper_id')->unsigned();
$table->string('student_id',14);
Students can have multiple results for a test (provided they are from different papers).
What I essentially need to do is, get any results where paper_id is one of test->paper->id where matching student ID (<|>|=) grade boundary but I do not want to have to do this for every student one by one.
Then I need to have a count of any students who were of a set grade.
Hope this makes sense?
Thanks!
Edit: Clarity
Looking to first Sum(any results with matching student_id, and that match any of an array of paper_id) then count(how many students were greater than a grade boundary)
something like (obviously this doesn't work correctly):
$count = Result::whereIn('paper_id', $paper_ids)->where('mark', '>', $grade_boundary)->count();
For anyone that is interested here is my not so elegant solution
$boundaries = $this->getBoundaries($test_id);
$count = [];
$test = Test::where('id', $test_id)->with('papers')->first();
$paper_ids = $test->papers->pluck('id');
$results = Result::whereIn('paper_id', $paper_ids)->get();
$student_ids = $results->pluck('student_id')->unique();
$externals = External::where('test_id', $test_id)->get();
$student_results = collect([]);
for ($i = 0; $i < count($student_ids); $i++) {
$temp = collect();
$temp->put('mark', $results->where('student_id', $student_ids[$i])->sum('mark'));
$student_results->push($temp);
}
for ($i = 0; $i < count($boundaries); $i++) {
$temp_count = 0;
if ($i === 0 && $i < count($boundaries)) {
$temp_count = $student_results->where('mark', '<', $boundaries[$i + 1]->mark)->count();
} elseif ($i === (count($boundaries)-1)) {
$temp_count = $student_results->where('mark', '>=', $boundaries[$i]->mark)->count();
} else {
$temp_count = $student_results->where('mark', '>=', $boundaries[$i]->mark)->where('mark', '<', $boundaries[$i + 1]->mark)->count();
}
//return $externals->where('grade_id', $boundaries[$i]->grade_id)->pluck('count');
$count[$i] = [
'grade' => $boundaries[$i]->name,
'count' => $temp_count,
'external' => $externals->where('grade_id', $boundaries[$i]->grade_id)->pluck('numbers')->first(),
];
}
return $count;
Please or to participate in this conversation.