Reputation: 49
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.
iam starting to think i need to combine the total valuations by total instructions to get my %. Any ideas how i can do this?
$query= "SELECT *, concat(round(( instructions/valuations * 100 ),0),'%') AS percentage FROM office_figures2016"; $result = mysqli_query($conn, $query); while($office_figures2016=mysqli_fetch_assoc($result)){
it echos :
echo"".$office_figures2016['percentage']."";
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
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