Trying to generate a table summing totals etc from a query but having trouble getting it to work.
I have this code:
<?php
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
$Renewed = "SUM(CASE WHEN Outcome = 'Renewed' THEN 1 ELSE 0 END) AS 'Renewed <br/> Cases'";
$Lapsed = "SUM(CASE WHEN Outcome = 'Lapsed' THEN 1 ELSE 0 END) AS 'Lapsed <br/> Cases'";
$Open = "SUM(CASE WHEN Outcome = 'Open' THEN 1 ELSE 0 END) AS 'Outstanding <br/> Cases'";
$Total = "SELECT COUNT(Assigned) as 'Total <br/> Assigned";
echo GenerateTable("SELECT COUNT(Assigned) as 'Total <br/> Assigned, $Open, $Renewed, $Lapsed FROM Data WHERE Assigned = '$assigned' ");
?>
and am getting this error:
MySQL ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Open’ THEN 1 ELSE 0 END) AS ‘Outstanding
Cases’, SUM(CASE WHEN Outcome = ‘ at line 1
and can’t spot where I’m going wrong.
In addition to this, I’m going to need a renewal ‘conversion’ % based off renewed cases to Total assigned cases. I tried
$Conversion = $Renewed/$Total;
but that didn’t make it that far, possibly because of the original error above. I will also need to round that to the nearest whole number for the percentage, i.e. (95.67868786% would equal 96%).
Really not sure how to get this working so any help would be greatly appreciated, thankyou in advance!
You missed a single quote (after
Total <br /> Assigned):GenerateTable("SELECT COUNT(Assigned) as 'Total <br/> Assigned', $Open, $Renewed, $Lapsed FROM Data WHERE Assigned = '$assigned' ");As for the percentage, you probably want to do that in PHP, that will be the simplest solution in this case. So if you have a row of your result set you can do something like:
$Conversion = $row['Renewed <br/> Cases] / $row['Total <br/> Assigned'];