-- 【指标卡】


SELECT
    SUM(IFNULL(trans_household_num,0)) AS 移交户数,
    SUM(IFNULL(trans_amt,0))/10000 AS 移交金额,
    SUM(IFNULL(payment_coll_num,0)) AS 解决户数,
    SUM(IFNULL(payment_coll_amt,0))/10000 AS 解决金额,
    (SUM(IFNULL(trans_amt,0)) - SUM(IFNULL(payment_coll_amt,0)))/10000 AS 未解决金额,
    SUM(IFNULL(trans_household_num,0)) - SUM(IFNULL(payment_coll_num,0)) AS 未解决户数,
    SUM(IFNULL(payment_coll_amt,0)) / SUM(IFNULL(trans_amt,0)) AS 回款解决率,
    SUM(IFNULL(payment_coll_num,0)) / SUM(IFNULL(trans_household_num,0)) AS 销户率
FROM dw.dws_law_tran_coll_d
WHERE left(ymd,7) = '2024-08'
    WHERE left(ymd,7) <= '${p_ym}%'
${IF(LEN(p_area)!=0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${if(left(fine_role,2)=="项目","and comm_name = '"+G
ETUSERDEPARTMENTS(3)+"'","")}


-- 【未移交款项】


SELECT
    ${IF(LEN(p_area)>0,"project_name","district_name")} AS 分析维度,
    SUM(IFNULL(owed_amt,0))/10000 AS 未决账款总额,
    SUM(IFNULL(morethan_24m_not_trans_amt,0))/10000 AS 往期欠费账龄24月以上未移交账款金额,
    SUM(IFNULL(owed_num,0)) AS 未决账款户数,
    SUM(IFNULL(morethan_24m_not_trans_num,0)) AS 往期欠费账龄24月以上未移交户数
FROM dws_law_not_tran_payment_m
WHERE 1=1
    ${IF(LEN(p_area)!=0," AND district_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
      ${if(left(fine_role,2)=="项目","and project_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
GROUP BY 分析维度
order by 未决账款总额 desc