Standard deviation result in php from values in SQL database
Get the solution ↓↓↓Having problem getting the right data to calculate each person standard devation. If the student does not have any grades, his or her name will not be displayed. Also, the query should not return students who do not have any grades. Is my query wrong or am I trying to get wrong data in result.php first foreach loop?
Data.sql
Student table(id INTEGER PRIMARY KEY, name VARCHAR(255) UNIQUE);
grade table(id INTEGER, grade INTEGER);
Student table Grade table
id name id grade
1 Alice 1 5
2 Bob 1 4
3 Carol 1 5
2 4
result.php
<?php
$conn = getConnectionWithData('data.sql');
$stmt = $conn->prepare('select * from student left join grade on grade.id = student.id' );
$stmt->execute();
function standardDeviation($inputList) {
$count = count($inputList);
$average = array_sum($inputList) / $count;
$variance = 0.0;
foreach($inputList as $each) {
$variance += pow($each - $average, 2);
}
return round(sqrt($variance / $count), 2);
}
$result = [];
foreach ($stmt as $row){
var_dump($row['grade']);
$grade = $row['grade'];
if(isset($result[$grade])){
$result[$grade] += $row['grade'];
}else{
$result[$grade] = $row['grade'];
}
}
foreach ($result as $name => $grades) {
$std = standardDeviation($grades);
print "$name: $std\n";
}
I should get answers as: Alice: 0.47 Bob: 0 but for me it shows Alice: 0, Bob: 0 and includes Carol: 0 as well.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: regex stop at first match
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.