-- 【1_report_空间运营出租率明细】
SELECT
    ${IF(LEN(p_area)>0,"A.comm_name", "A.organ_name")} AS NAME
    ,SUM(A.POINT_NUM           ) AS POINT_NUM       -- 点位数量
    ,SUM(B.point_num_total     ) AS point_num_total-- 总点位数
    ,SUM(A.IS_ADD              ) AS add_point_num   -- 新增点位数
    ,SUM(B.point_num_valid     ) AS point_num_valid   -- 有效点位数
    ,SUM(B.point_num_invalid   ) AS point_num_invalid -- 无效点位数
    ,SUM(B.point_num_rent      ) AS point_num_rent   -- 出租点位数
    ,SUM(A.Price               ) AS Price -- 单价
    ,CASE WHEN  SUM(B.point_num_total     )  = 0 THEN 0
          ELSE SUM(B.point_num_valid     ) / SUM(B.point_num_total     ) END AS point_rent_rate -- 考核出租率
FROM
(
    SELECT
      A2.organ_code AS organ_code  -- 片区ID
     ,A2.organ_name AS organ_name  -- 片区名称
     ,A1.CommID     AS CommID      -- 项目id
     ,A2.comm_name  AS comm_name   -- 项目名称
     ,A1.IS_ADD     AS IS_ADD
     ,A1.POINT_NUM  AS POINT_NUM
     ,A1.New_Price  AS New_Price
     ,A1.New_Price /  A1.POINT_NUM AS Price
    FROM
    (
        SELECT
         CommID     -- 项目id
        ,SUM(CASE WHEN PriceUnit like '%月%' THEN IFNULL(Price,0) * 12 ELSE IFNULL(Price,0) END) AS New_Price
        ,SUM(CASE WHEN AddTime >= DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), INTERVAL 1 DAY) THEN 1
                 ELSE 0 END) AS IS_ADD
  --    ,SUM(CASE WHEN AddTime >= CONCAT('${p_ym}','-01') THEN 1
  --                            ELSE 0 END) AS IS_ADD
        ,count(1)    AS POINT_NUM -- 点位数量
        FROM dw.dim_innoveco_space_point_d -- 创新生态空间点位
        GROUP BY
            CommID
     )A1
     LEFT JOIN
     (
        SELECT
         comm_id
        ,comm_name
        ,organ_code
        ,organ_name
        FROM dw.dim_organ_mapping
        GROUP BY
        comm_id
        ,comm_name
        ,organ_code
        ,organ_name
     )A2
  --  ON A1.OrganCode = A2.organ_code
    ON A1.CommID = A2.comm_id
)A
LEFT JOIN
(
    SELECT
     organ_code           -- 片区ID
    ,organ_name          -- 片区名称
    ,comm_id             -- 项目ID
    ,comm_name           -- 项目名称
  --   ,usestate            -- 项目状态
   -- ,point_type          -- 点位类型
    ,SUM(IFNULL(point_num_total    ,0)) AS point_num_total    -- 总点位数
    ,SUM(IFNULL(point_num_ismustdo ,0)) AS point_num_ismustdo -- 必做点位数
    ,SUM(IFNULL(point_num_zengzhi  ,0)) AS point_num_zengzhi  -- 增值点位数
    ,SUM(IFNULL(point_num_valid    ,0)) AS point_num_valid    -- 有效点位数
    ,SUM(IFNULL(point_num_invalid  ,0)) AS point_num_invalid  -- 无效点位数
    ,SUM(IF(usestate='使用',IFNULL(point_num_total,0),0)) AS point_num_rent -- 出租点位数
    FROM dw.dws_innoveco_point_summary   -- 有效点位
    GROUP BY
        organ_code           -- 片区ID
           ,organ_name          -- 片区名称
           ,comm_id             -- 项目ID
           ,comm_name           -- 项目名称
)B
ON A.CommID = B.comm_id
AND A.organ_code = B.organ_code
WHERE B.comm_id IS NOT NULL
${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${if(left(fine_role,2)=="项目","and A.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
GROUP BY
${IF(LEN(p_area)>0,"A.comm_name", "A.organ_name")}