0

尝试从查询中生成汇总总计等的表格,但无法使其正常工作。

我有这个代码:

    <?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'  ");

?>

并收到此错误:

MySQL 错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在 'Open' THEN 1 ELSE 0 END) AS 'Outstanding Cases', SUM(CASE WHEN Outcome = ' at line 1 附近使用

并且无法发现我哪里出错了。

除此之外,我还需要根据续订案件到分配案件总数的续订“转换”百分比。我试过了

$Conversion = $Renewed/$Total;

但这并没有那么远,可能是因为上面的原始错误。我还需要将其四舍五入到最接近的百分比整数,即(95.67868786% 等于 96%)。

真的不知道如何让这个工作,所以任何帮助将不胜感激,在此先感谢您!

4

1 回答 1

2

您错过了一个单引号(在 之后Total <br /> Assigned):

GenerateTable("SELECT COUNT(Assigned) as 'Total <br/> Assigned', $Open, $Renewed, $Lapsed FROM Data WHERE Assigned = '$assigned' ");

至于百分比,您可能想在 PHP 中执行此操作,在这种情况下,这将是最简单的解决方案。因此,如果您有一行结果集,您可以执行以下操作:

$Conversion = $row['Renewed <br/> Cases] / $row['Total <br/> Assigned'];

于 2012-10-08T14:02:18.197 回答