0

I have created a table in the database which displays Date, User, Value. The following is a revised design:

   Date    | User  | Value  
---------- | ----- | -----    

So each user has a count of what submissions they have done matching via the date. The results are displayed from this query from the database

SELECT * FROM testreport

Please see the new design below as mentioned from the comments, I have added the row total at the end.

   Date    | User1 | User2 | User3 |    Value 
---------- | ----- | ----- | ----- | -----------
23-02-2017 |   2   |   5   |   9   |     16
24-02-2017 |   5   |   7   |   11  |     23   
25-02-2017 |   6   |   13  |   1   |     20

But what I am trying to achieve is by selecting the table and have total to show in the last row like this, how do I go about doing this:

   Date    | User1 | User2 | User3 |    Value 
---------- | ----- | ----- | ----- | -----------
23-02-2017 |   2   |   5   |   9   |     16
24-02-2017 |   5   |   7   |   11  |     23   
25-02-2017 |   6   |   13  |   1   |     20
Total      |   13  |   25  |   21  |     59

but I cannot specify the column names like How do I calc a total row using a PIVOT table, without UNION, ROLLUP or CUBE?. Because from time time there will be extra users added to this table but I want to be able to get all the columns with totals at the end. On my site I am using PHP to display the table on the page with the code:

$sqlres = "SELECT * FROM testreport order by Date DESC ";
$resultsql = mysqli_query($sqlres) or die(mysql_error());

echo "<table><tr>";
for($i = 0; $i < mysqli_num_fields($resultsql); $i++) {
    $field_info = mysqli_fetch_field($resultsql, $i);
    echo "<th>{$field_info->name}</th>";
}

// Print the data
while($rows = mysqli_fetch_row($resultsql)) {
    echo "<tr>";
    foreach($rows as $_column) {
        echo "<td>{$_column}</td>";
    }
    echo "</tr>";
}

echo "</table>";

Which displays the headers and rows as I wanted them to be and any new user I add to the 'testreport' table column it automatically adds an extra column on the page, how do I go about achieving the rollup of these columns to get total.

Any help on this would be appreciated.

Community
  • 1
  • 1
TeyJohn
  • 11
  • 4

0 Answers0