-- 【report_指标卡_填报】

SELECT
-- 收入汇总费用
 SUM(CASE WHEN business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END) as revenue_value -- 四大业务收入
,SUM(CASE WHEN revenue_type = '附属' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END) AS revenue_value_1 -- 空间运营收入
,SUM(CASE WHEN revenue_type = '拎包' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END) AS revenue_value_2 -- 美居收入
,SUM(CASE WHEN revenue_type = '创新' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END) AS revenue_value_3 -- 新零售收入
,SUM(CASE WHEN revenue_type = '资产' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END) AS revenue_value_4 -- 资产出租收入

-- 收入完成率
,CASE WHEN SUM(CASE WHEN business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
      ELSE SUM(CASE WHEN business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
 / SUM(CASE WHEN business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
      END AS finish_rate -- 四大业务收入完成率

,CASE WHEN SUM(CASE WHEN revenue_type = '附属' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
      ELSE SUM(CASE WHEN revenue_type = '附属' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
/ SUM(CASE WHEN revenue_type = '附属' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
      END AS finish_rate_1 -- 空间运营收入完成率

,CASE WHEN SUM(CASE WHEN revenue_type = '拎包' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
      ELSE SUM(CASE WHEN revenue_type = '拎包' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
/ SUM(CASE WHEN revenue_type = '拎包' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
      END AS finish_rate_2 -- 美居收入完成率

,CASE WHEN SUM(CASE WHEN revenue_type = '创新' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END) = 0 THEN 0
      ELSE SUM(CASE WHEN revenue_type = '创新' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
/ SUM(CASE WHEN revenue_type = '创新' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
      END AS finish_rate_3 -- 新零售收入完成率

,CASE WHEN SUM(CASE WHEN revenue_type = '资产' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)  = 0 THEN 0
      ELSE SUM(CASE WHEN revenue_type = '资产' AND business_type = '收入' THEN COALESCE(revenue_value,0) ELSE 0 END)
/ SUM(CASE WHEN revenue_type = '资产' AND business_type = '目标' THEN COALESCE(revenue_value,0) ELSE 0 END)
      END AS finish_rate_4 -- 资产出租收入完成率

FROM  dw.ods_innoveco_metrics_finish_d
WHERE business_type in('收入','目标') -- 业务类型
AND YM = '${p_ym}'
${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
;


-- report_指标卡


    WITH T1 AS (/*各类别应收与实收*/
SELECT
	index_type,
	SUM(IFNULL(index_receivable,0)) AS 应收金额,
	SUM(IFNULL(index_receipts,0)) AS 实收金额
FROM ods_caiwu_profit
WHERE 1=1
--	AND `year_month` = '${REPLACE(p_ym,"-","")}'
  --  ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
 --   ${IF(LEN(p_prgject)>0," AND comm_id IN ('"+JOINARRAY(p_prgject,"','")+"')", "")}
    GROUP BY index_type
    )
SELECT
    SUM(CASE WHEN index_type = '附属资源/空间运营' THEN 实收金额 ELSE 0 END) AS 空间运营实收,
    SUM(CASE WHEN index_type = '附属资源/空间运营' THEN 实收金额 / 应收金额 ELSE 0 END) AS 空间运营完成率,
    SUM(CASE WHEN index_type = '美居' THEN 实收金额 ELSE 0 END) AS 美居实收,
    SUM(CASE WHEN index_type = '美居' THEN 实收金额 / 应收金额 ELSE 0 END) AS 美居完成率,
    SUM(CASE WHEN index_type = '社商/新零售' THEN 实收金额 ELSE 0 END) AS 新零售实收,
    SUM(CASE WHEN index_type = '社商/新零售' THEN 实收金额 / 应收金额 ELSE 0 END) AS 新零售完成率,
    SUM(CASE WHEN index_type = '资产运营/资产租售' THEN 实收金额 ELSE 0 END) AS 资产出租实收,
    SUM(CASE WHEN index_type = '资产运营/资产租售' THEN 实收金额 / 应收金额 ELSE 0 END) AS 资产出租完成率,
    SUM(实收金额) AS 全部实收,
    IFNULL(SUM(实收金额) / SUM(应收金额),0) AS 完成率
FROM T1
;

