-- ======================================
-- report_5_完成率
-- ======================================

SELECT
    organ_code AS 片区id,
    organ_name AS 片区名称,
    SUM(IFNULL(incident_num,0)) AS 报事总数,
    IFNULL((SUM(IFNULL(response_timely_incident_num,0)) / SUM(IFNULL(incident_num,0))),0) AS 及时响应率,
    IFNULL((SUM(IFNULL(completed_incident_num,0)) / SUM(IFNULL(incident_num,0))),0) AS 完成率,
    IFNULL((SUM(IFNULL(follow_up_incident_num,0)) / SUM(IFNULL(incident_num,0))),0) AS 回访率,
    IFNULL((SUM(IFNULL(incident_close_num,0)) / SUM(IFNULL(incident_num,0))),0) AS 闭单率,
    case when   (sum(incident_num) - sum(indeal_incident_num)) = 0 THEN 0
        else sum(incident_close_num) / (sum(incident_num) - sum(indeal_incident_num))
        end AS 有效闭单率
FROM dw.dws_estate_incident_info_m T1
WHERE 1=1
    ${IF(LEN(p_startdate)>0,"AND T1.incident_ym = '"+FORMAT(CONCATENATE(p_startdate,"-01"),"yyyyMM")+"'","")}
      ${IF(LEN(p_area)>0," AND T1.organ_code IN ('"+p_area+"')", "")}
GROUP BY organ_code,organ_name