Glen
Glen

Reputation: 49

echoing an AVG value from a virtual value with mysql and php

I have the following query and it gives me the percentage. It works great and i echo it in a table. In this table i also want to echo the AVG % as a total.i.e i have 12 months of year and want the avg. % for the year at the bottom of the table. I have worked out how to do this for valuations and instructions that are stored in my database. But.. how do i do this for a 'virtual' column like %..as this has been created from a query and not a physical column in the database.

and here is the php to get the totals for each column...the third part of code is wrong but this is what i have so far:

<?php
$sql = "SELECT ROUND(AVG(valuations),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";


$sql = "SELECT ROUND(AVG(instructions),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";


// need to divide total valuations by total instructions x 100 = % 
// somehow need to combine the value sums together..  if? 

$sql = "SELECT ROUND(AVG(''),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";

Upvotes: 0

Views: 80

Answers (1)

Glen
Glen

Reputation: 49

I redid the formula to divide all instructions and valuations in the table x100 = %.   

$sql = "SELECT ROUND(AVG(instructions / valuations *100 ),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";

Upvotes: 0

Related Questions