2013-05-05 40 views
0

如何將COUNT的結果回顯到名爲「已解決,重新分配和關閉」的三列中?如何計數並將結果回顯到不同的列

我想計算已解決,重新分配和關閉的票數,並將它們放置到其相應的列。我還需要總結每個列和行的總數。

這是一個售票系統,它將通過Severity,Category 2,Category 3和Status進行上傳。

所以在下面的圖片中,報告將在每列中生成唯一值,因此對於嚴重性5,我們有兩個條目在類別2下,並且對於每個類別2在類別3下具有不同的條目,那麼我想展示許多票據已被解決,重新分配和關閉在類別3下的每個條目,例如SMP_Backend在嚴重度5下有一個待處理請求,其他處於類別3下,因此對於待處理訂單可以說我們已經解決了3並且重新分配了2個。要顯示解決和重新分配列下這些數字在掛起的請求行,我也想顯示每行和每列總...

這裏是圖像:

enter image description here

這裏是我的代碼:

<?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> 
+1

您的代碼易受SQL注入的影響。 – j883376 2013-05-05 12:14:56

+0

您提供的信息太少。確定票是否被重新分配需要什麼邏輯?如果在同一天重新分配和解決票(以及爲什麼不關閉),應該發生什麼?請編輯你的問題來澄清,最好在你的問題中包含太多的信息而不是太少。 – hvd 2013-05-05 12:18:52

+0

更多信息現在提供...希望其清晰已經... – 2013-05-05 12:32:12

回答

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 
相關問題