如何将COUNT的结果回显到我的三个名为“已解决,重新分配和已关闭”的列?
我想计算已解决,已重新分配和已关闭的票证的数量,并将它们放入相应的列。我还需要总结每列和每行的总和。
这是一个票务系统,将通过严重性,类别2,类别3和状态上传。
所以在下图中,将生成每列中具有唯一值的报告,因此对于严重性5,我们在类别2下有两个条目,并且对于每个类别2,在类别3下将有不同的条目然后我想要显示多少对于类别3下的每个条目,票证已经解决,重新分配和关闭,例如,严重性5下的SMP_Backend有一个待处理请求而其他类别属于类别3,因此对于待处理订单,我们说已经解决了3并重新分配了2.我想要在Pending Request行中的Resolved和Re-assigned列下显示这些数字,我还想显示每行和每列的总数......
这是图像:
这是我的代码:
<?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 = ' ';
$prCat2 = $category_1;
$prCat3 = $category_2;
$prev2 = $category_1;
$prev3 = $category_2;
}
elseif ($category_2 != $prev3) {
$prCat1 = ' ';
$prCat2 = ' ';
$prCat3 = $category_2;
$prev3 = $category_2;
}
else $prCat1 = $prCat2 = $prCat3 = ' ';
$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"> </td>
<td width="68" style="display:none"> </td>
<td width="144" style="display:none"> </td>
<td width="56" style="display:none"> </td>
<td width="34" style="display:none"> </td>
<td width="93" style="display:none"> </td>
<td width="107" style="display:none"> </td>
</tr>
<tr>
<td colspan="6" align="center">Total</td>
<td align="left"> </td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:0)
您要求的内容称为 pivot 查询。根据您的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