I am trying to COUNT the same field twice, as the current setup inputs work codes as '10', '11', '12', '13', '40', '400', '80' as completed jobs, however other works codes likes 55, 56, 57 means a job was not completed.
With the following I've managed to count the work_code table and display the amount of successful job completed, but i am struggling on how to count the same table for jobs not completed (ie 55, 56 etc) as display that also.
Below is my code.....
<?php
require 'db.php';
$sqltran = mysqli_query($con, "SELECT *, ROUND(SUM(Sales),0), ROUND(AVG(Sales),2), COUNT(Work_Code) FROM results_tbl_1 WHERE RECORD_ID BETWEEN 468100 AND 500000 AND Work_Code IN ('10', '11', '12', '13', '40', '400', '80') GROUP BY ENG_ID ORDER BY ROUND(SUM(Sales),0) DESC ") or die(mysqli_error($con));
$arrVal = array();
$i=1;
while ($rowList = mysqli_fetch_array($sqltran)) {
$name = array(
'num' => '<img alt="" height="16" src="./top10.png" width="16">' . $i,
'eng'=> $rowList['ENG_ID'],
'totalvisits'=> $rowList['COUNT(Work_Code)'],
'ajv'=> '£' . $rowList['ROUND(AVG(Sales),2)'],
'sales'=> '£' . $rowList['ROUND(SUM(Sales),0)'],
);
array_push($arrVal, $name);
$i++;
}
echo json_encode($arrVal);
mysqli_close($con);
The data is then displayed on a bootstrap table with the below config..
<script type="text/javascript">
var $table = $('#table');
$table.bootstrapTable({
url: 'test/board.php',
search: false,
pagination: true,
buttonsClass: 'primary',
showFooter: false,
minimumCountColumns: 2,
columns: [{
field: 'num',
title: 'Rank',
sortable: false,
},{
field: 'eng',
title: 'Engineer',
sortable: false,
},{
field: 'totalvisits',
title: 'Total Jobs',
sortable: false,
},{
field: 'ajv',
title: 'AJV',
sortable: false,
},{
field: 'sales',
title: 'Total',
sortable: false,
}, ],
});
My question is how can i count the work codes 55,56 etc and display the count on the table ?
Thanks :)
Create two new arrays, one for job codes that indicate completion, another for job codes that indication incomplete. Also, don't try to do the count in your query in this case.
Then, as you're looping through your results, test $row['Work_Code'] against the arrays using
array_search()`. You'll also need two variables to store the counts.
It would look something like this:
$completedJobs = array(1,2,3,4,5);
$incompleteJobs = array(7,8,9,10,11);
$completeTotal = 0;
$incompleteTotal = 0;
$i=1;
while ($rowList = mysqli_fetch_array($sqltran)) {
//I've stripped out what is not relevant to my example
if(array_search($rowList['Work_Code'], $completedJobs) {
$completeTotal++;
}
if(array_search($rowList['Work_code'],$incompleteJobs){
$incompleteTotal++;
}
}
Just run 2 separate queries, with different values in the IN
clause:
Completed jobs:
<?php
require 'db.php';
$sql_complete = mysqli_query($con, "SELECT *, ROUND(SUM(Sales),0), ROUND(AVG(Sales),2), COUNT(Work_Code) FROM results_tbl_1 WHERE RECORD_ID BETWEEN 468100 AND 500000 AND Work_Code IN ('10', '11', '12', '13', '40', '400', '80') GROUP BY ENG_ID ORDER BY ROUND(SUM(Sales),0) DESC ") or die(mysqli_error($con));
while ($rowList = mysqli_fetch_array($sql_complete)) {
// Outputs the number of completed jobs:
echo $rowList['COUNT(Work_Code)'];
}
?>
Incomplete jobs:
<?php
$sql_incomplete = mysqli_query($con, "SELECT *, ROUND(SUM(Sales),0), ROUND(AVG(Sales),2), COUNT(Work_Code) FROM results_tbl_1 WHERE RECORD_ID BETWEEN 468100 AND 500000 AND Work_Code IN ('55', '56', '57' ) GROUP BY ENG_ID ORDER BY ROUND(SUM(Sales),0) DESC ") or die(mysqli_error($con));
while ($rowList = mysqli_fetch_array($sql_incomplete)) {
// Outputs the number of incomplete jobs:
echo $rowList['COUNT(Work_Code)'];
}
?>
With assumption that all codes that are not in "completed jobs" codes list are incomplete, the query could be as simple as this:
SELECT *, ROUND(SUM(IF(complete, Sales, null)),0), ROUND(AVG(IF(complete, Sales, null)),2), COUNT(IF(complete, 1, null)), COUNT(IF(complete, null, 1))
FROM ( SELECT *, Work_Code IN ('10', '11', '12', '13', '40', '400', '80') as complete
FROM results_tbl_1
) separated
WHERE RECORD_ID BETWEEN 468100 AND 500000
GROUP BY ENG_ID
ORDER BY ROUND(SUM(Sales),0) DESC
A few things not related to the question:
SELECT *,
in aggregated queries So the better version of the query is:
SELECT
ENG_ID,
ROUND(SUM(IF(complete, Sales, null)),0) as Total_sales_complete,
ROUND(AVG(IF(complete, Sales, null)),2) as Average_sales_complete,
COUNT(IF(complete, 1, null)) as Count_complete,
COUNT(IF(complete, null, 1)) as Count_incomplete
FROM ( SELECT *, Work_Code IN ('10', '11', '12', '13', '40', '400', '80') as complete
FROM results_tbl_1
) separated
WHERE RECORD_ID BETWEEN 468100 AND 500000
GROUP BY ENG_ID
ORDER BY Total_sales_complete DESC
EDIT:
The only changes in php code is to use aliases:
$name = array(
'num' => '<img alt="" height="16" src="./top10.png" width="16">' . $i,
'eng'=> $rowList['ENG_ID'],
'totalvisits'=> $rowList['Count_complete'],
'ajv'=> '£' . $rowList['Total_sales_complete'],
'sales'=> '£' . $rowList['Average_sales_complete'],
'theSecondCount'=> $rowList['Count_incomplete'],
);
And on javascript side add the extra column:
{
field: 'theSecondCount',
title: 'Incomplete Jobs',
sortable: false,
}
As a side note, the title "Total Jobs" is a bit misleading, as it shows count for complete jobs, and the word "Total" implies all jobs.
也许您想将UNION用于具有相应工作代码的两个SELECT。
我可能会遗漏明显的内容,但为什么不在“ work_code in”子句中包含未完成的代码?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.