我有一个带有一些评分的 SQL 查询,例如
location, cleanliness, money, kitchen, checkinprocess, servicebyowner
对于很少的数据checkinprocess
,servicebyowner
都是0
。我只想计算这些值的平均值。
我想检查我的查询,例如,如果有 20 行作为结果,则必须仅针对现有行计算平均值checkinprocess
(serviceby owner
如果仅针对 10 行结果中的 2 行),它必须仅计算 2 行的平均值,但对于其他行,它可以根据行数计算。这是我写的查询:
select count(customdata) as CNT,
customdata,
sum(service_by_owner) as ser,
sum(checkinprocess) as chk,
sum(locationrating) as loc,
sum(cleanlinessrating) as cle,
sum(valueformoneyrating) as val,
sum(kitchenequipmentrating) as kit
from feedback
group by customdata HAVING customdata !='null' AND customdata!='';
我可以根据行计算其余四个字段的平均值,但这两个字段对我来说有点棘手。
编辑:
PHP用于计算平均值:
foreach($result as $row){
$arrValues[] = array("customdata"=>$row['customdata'],
"checkinprocess"=>ceil(($row['chk'])),
"service_by_owner"=>(ceil($row['ser'])),
"valueformoneyrating"=>ceil(($row['val']/($row['CNT']*4))*100),
"locationrating"=>ceil(($row['loc']/($row['CNT']*4))*100),
"kitchenequipmentrating"=>ceil(($row['kit']/($row['CNT']*4))*100),
"cleanlinessrating"=>ceil(($row['cle']/($row['CNT']*4))*100));
}
这个 checkinprocess 和 servicebyowner,我必须只计算现有行的平均值,而不是所有为 0 的行。