Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找php和mysql的平均完成百分比_Php_Mysql - Fatal编程技术网

查找php和mysql的平均完成百分比

查找php和mysql的平均完成百分比,php,mysql,Php,Mysql,我在名为customer\u type\u total\u avg的表中有这个结构 USEDID CID TYPE PERCENTAGECOMPLETED ------- ---- ---- ------------------ 1 6 external 12 1 6 external

我在名为customer\u type\u total\u 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查询来查找USERID和CID匹配的行的总平均值

注意:我们还应该在SELECT查询中包含以下条件以获得平均值

当“类型”是“外部”时,应该考虑列中的值“%/P>>/LI>”。 >P>当“类型”是“内部”和“百分率”=“50”时,它应该替换百分数的值=“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

您可以使用
案例

SELECT AVG(CASE WHEN TYPE='internal' and PERCENTAGECOMPLETED=50 
                THEN 0 
                ELSE PERCENTAGECOMPLETED END) AS AVG
FROM CUSTOMER_TYPE_TOTAL_AVG

您尝试过的代码在哪里?先发布代码我发布了@Santhosh@ShashankShah你明白我需要什么吗!根据您的要求,我认为您根本不需要使用AVG()。欢迎!如果这有助于解决您的问题,请通过单击勾选选项将此答案标记为正确,我将不胜感激@阿鲁尼@阿伦宫殿酒店