0

Having an issue with the total count of column records with the group by query. my data should be shown like:

Call Status               Call Results  Percentage
Answering Machine         1             5.6%
DO NOT CALL               9             50.0%
Good Transfer             4             22.2%
Looking for Information   4             22.2%
(blank)                                 0.0%
Total Calls               18    

I am using the following query to do my work. But yet i am taking hardcore "18" because the problem occurs here that how i can take whole column count with group by.

$result = mysqli_query($link, "SELECT vs.status_name,vl.call_date,SUM(vl.called_count) as Total,SUM(vl.called_count)/18*100 as percentage from vl_users vu,".$vicidial_log_table." vl,vicidial_list vi,vicidial_statuses vs where vl.call_date >= '2014-01-17 00:00:00' and vl.call_date <= '2016-10-17 23:59:59' group by vs.status_name limit 100000") or die(mysqli_error($link));

The number 18 above is the sum of the call result column.

9
  • 1
    It this MySQL or SQL Server? Please be more careful with your tags. Commented Oct 17, 2016 at 10:37
  • mysqli_query - MySQL Commented Oct 17, 2016 at 10:38
  • This is Mysql. i have removed sql server tag. Commented Oct 17, 2016 at 10:38
  • Possible duplicate of How do I add a total as the last row in my sql? Commented Oct 17, 2016 at 10:39
  • But Actually @DavidG i don't have ny null value in the table. Commented Oct 17, 2016 at 10:42

1 Answer 1

1

Right 3 ways of getting round this issue.

  1. I would do the sum query first and then just add the sum into the percentage calc.

  2. Might take a bit longer to calculate - in place of the 18 do a subquery to return the sum.

  3. Don't use mysql to calculate the percentage. Instead use php to sum the array and then calc the percentage in each row while it's displaying.

1 - would be my recommendation.

Edit Ok, given the complexity of the query it seems 3 is the answer. So how to do it.

$result = mysqli_query($link, "SELECT vs.status_name,vl.call_date,SUM(vl.called_count) as Total from vl_users vu,".$vicidial_log_table." vl,vicidial_list vi,vicidial_statuses vs where vl.call_date >= '2014-01-17 00:00:00' and vl.call_date <= '2016-10-17 23:59:59' group by vs.status_name limit 100000") or die(mysqli_error($link));

$all_results = mysqli_fetch_all($result);

//might be a quicker way but
$sum = 0;
foreach($all_results as $s) {
    $sum  = $sum + $s['Total'];
}


//now to print
echo '<table><tr> <td>Call Status</td><td>Call Results</td><td>Percentage</td></tr>';

foreach($all_results as $row) {

   echo '<tr>';
echo '<td>'.$row['status_name'].'</td>';
echo '<td>'.$row['Total'].'</td>';

$percentage = 0;
if($sum > 0 && $row['Total'] > 0){
   $percentage = ($row['Total']/$sum)*100;
}
echo '<td>'.$percentage.'%</td>';


   echo '</tr>';




}

echo '</table>';

I just add that this is how I would do it, seems ok. I guess test and see.

Sign up to request clarification or add additional context in comments.

10 Comments

Why? Calculate the sum of the rows first and then plug that in. Why wouldn't it work?
How i can access the sum of column sum inside the same query. It will cause Error.
@junkk rr: Like so: select a, sum(b), (select sum(b) from t) as total from t group by a.
The 3 solutions are correct and I would go 1 as well. Just throw the first query that Thorsten linked. Store the result in a PHP variable. Then when you prepare the scond query (your initial query), just change the 18 with that variable. Easy.
Why do you all prefer solution 1? Is MySQL's optimizer supposed to be so bad that it would run this non-correlated subquery multiple times? That is hard to imagine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.