0

我如何将 COUNT 的结果回显到名为:Resolved、Re-assigned 和 Closed 的三个列中?

我想计算已解决、重新分配和关闭的票证的数量,并将它们放在相应的列中。我还需要总结每一列和每一行的总计。

这是一个票务系统,将通过严重性、类别 2、类别 3 和状态上传。

所以在下图中,将生成每列中具有唯一值的报告,因此对于严重性 5,我们在类别 2 下有两个条目,并且每个类别 2 在类别 3 下都有不同的条目,然后我想显示已解决的票数, 为类别 3 下的每个条目重新分配和关闭,例如严重性 5 下的 SMP_Backend 有一个待处理请求,其他类别为 3,因此对于待处理订单,假设我们已解决 3 并重新分配 2。我想展示那些待处理请求行中已解决和重新分配的列下的数字,我还想显示每行和每列的总数...

这是图像:

在此处输入图像描述

这是我的代码:

    <?php
require 'include/DB_Open.php';

$dates = $_POST['dates'];

$sql ="SELECT trouble_type_priority as `Severity`
    , category_1
    , category_2
    , SUM(IF(status='Resolved', 1, NULL)) as `Resolved`
    , SUM(IF(status='Re-assigned', 1, NULL)) as `Re-assigned`
    , SUM(IF(status IN ('Closed','Suspended'), 1, NULL)) as `Closed`
    , COUNT(status) as `Total`
    FROM tbl_main
    WHERE resolved_date = '$dates'
    GROUP BY Severity, category_1, category_2";

$myData = mysql_query($sql)or die(mysql_error());

$output = 
"<tr>
    <th colspan='3' align='center'>Ticket Bucket</th>
    <th colspan='3' align='center'>Status</th>
</tr>
<tr>
    <th width='auto' align='center'>Severity</th>
    <th width='auto' align='center'>Category 2</th>
    <th width='auto' align='center'>Category 3</th>
    <th width='auto' align='center'>Resolved</th>
    <th width='auto' align='center'>Re-assigned</th>
    <th width='auto' align='center'>Closed</th>
    <th width='auto' align='center'>Grand Total</th>
</tr>\n";
$prev1 = $prev2 = $prev3 = '';
while (list($trouble_type_priority,$category_1,$category_2) = mysql_fetch_array($myData)) {

    if ($trouble_type_priority != $prev1) {
        $prCat1 = $trouble_type_priority;
        $prCat2 = $category_1;
        $prCat3 = $category_2;
        $prev1 = $trouble_type_priority; 
        $prev2 = $category_1;
        $prev3 = $category_2;
    }
    elseif ($category_1 != $prev2) {
        $prCat1 = '&nbsp';
        $prCat2 = $category_1;
        $prCat3 = $category_2;
        $prev2 = $category_1;
        $prev3 = $category_2;
    }
    elseif ($category_2 != $prev3) {
        $prCat1 = '&nbsp';
        $prCat2 = '&nbsp;';
        $prCat3 = $category_2;
        $prev3 = $category_2;
    }
    else $prCat1 = $prCat2 = $prCat3 = '&nbsp;';
    $output .= "<tr><td>$prCat1</td><td>$prCat2</td><td>$prCat3</td></tr>\n";
}
?>
<html>
<head>
<meta name="generator" content="PhpED Version 8.1 (Build 8115)">
<title>Example</title>
<meta name="author" content="Barand">
<meta name="creation-date" content="05/04/2013">
</head>
<body>
    <table border="0" cellpadding="1" >
    <tr>
    <th>Team Report</th>
    </tr>
    </table>
    <table border="0" cellpadding="1" >
    <tr>
    <th colspan='2'>Remaining Tickets:</th>
    </tr>
    <tr>
    <th width='72'>Wireless:</th>
    <th><input type='text' name='WirelessRemaining' id='WirelessRemaining' size='5' align='middle' /></th>
    </tr>
    <tr>
    <th>Wireline:</th>
    <th><input type='text' name='WirelineRemaining' id='WirelineRemaining' size='5' align='middle' /></th>
    </tr>
    </table>
    <table border="1" cellpadding="2">
        <?php echo $output?>
    </table>
    </table>
    <table border="1" cellpadding="1">
    <tr>
    <td width="34" style="display:none">&nbsp;</td>
    <td width="68" style="display:none">&nbsp;</td>
    <td width="144" style="display:none">&nbsp;</td>
    <td width="56" style="display:none">&nbsp;</td>
    <td width="34" style="display:none">&nbsp;</td>
    <td width="93" style="display:none">&nbsp;</td>
    <td width="107" style="display:none">&nbsp;</td>
    </tr>
    <tr>
    <td colspan="6" align="center">Total</td>
    <td align="left">&nbsp;</td>
    </tr>
    </table>
</body>
</html>
4

1 回答 1

0

您要求的内容称为数据透视查询。根据您的 DBMS,可以使用PIVOT运算符或CASE语句来完成。像这样的东西:

select id, resolved, closed, resolved + closed as total
from (
    select   id
           , max(case type when 'R' then Q else 0 end) as resolved
           , max(case type when 'C' then Q else 0 end) as closed
    from (
       select id, type, count(type) as Q
       from cases 
       group by id, type
    ) as A
) as B
于 2013-05-06T04:34:50.997 回答