我在名为 customer_type_total_avg 的表中有这个结构
USEDID CID TYPE PERCENTAGECOMPLETED
------- ---- ---- ------------------
1 6 external 12
1 6 external 50
1 6 internal 50
1 6 external 84
1 6 internal 100
1 6 external 100
function getCustomerprogress($where) {
$progress = 0;
$cid = $where["cid"];
$userid = $where["userid"];
$sql = "SELECT
TRUNCATE(avg(t.percentage),1) as avg_percentage
FROM
(SELECT
percentagecompleted as 'percentage'
FROM
customer_type_total_avg
WHERE
userid = $userid and cid = $cid) AS t";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$progress = $row["avg_percentage"];
}
}
return $progress;
}
我需要一个 SELECT 查询来查找 WHERE USERID 和 CID 匹配的行的总平均值
注意:我们也应该在 SELECT 查询中包含以下条件以获得平均值
当“类型”为“外部”时,应考虑“已完成百分比”列中的任何值
当“TYPE”为'internal'且percentagecompleted = '50'时,应替换percentagecompleted = '0'的值,并考虑求平均值的行
示例:从上表中,我们匹配了六行,它将考虑用于平均计算的外部类型百分比完成值,在第三行中,内部百分比完成的百分比为“50”,我需要将值替换为“0”和求总行数的平均值
USEDID CID TYPE PERCENTAGECOMPLETED
------- ---- ---- ------------------
1 6 external 12
1 6 external 50
1 6 internal 0
1 6 external 84
1 6 internal 100
1 6 external 100
翻过高山走不出你