<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>next_lvl_org_pm</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2025/02/27 12:55:12.966</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/02/27 12:55:12.966</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Table output</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH latest_hierarchy AS (
    -- Берем только записи с максимальной версией для каждого idm_id
    SELECT DISTINCT ON (idm_id) *
    FROM ervu_dashboard.recruitment
    ORDER BY idm_id, updated_at DESC
),
hierarchy_cte AS (
    -- Строим связи между уровнями (Department → Organization → Region → Ministry)
    SELECT 
        h.idm_id AS department_id,
        h.parent_id AS organization_id,
        h2.parent_id AS region_id,
        h3.parent_id AS ministry_id
    FROM latest_hierarchy h
    LEFT JOIN latest_hierarchy h2 ON h.parent_id = h2.idm_id -- Organization
    LEFT JOIN latest_hierarchy h3 ON h2.parent_id = h3.idm_id -- Region
    WHERE h.schema = 'Department' -- Начинаем с Department
),
aggregated_counts AS (
    -- Агрегируем для Organization
    SELECT 
        h.organization_id AS level_id, 
        SUM(r.reaching_17_age) AS total_reaching_17_age,
        SUM(r.women_military_specialty) AS total_women_military_specialty,
        SUM(r.count_all) AS total_count_all,
        recording_date AS recording_date, 
        'Organization' AS level
    FROM forecast.registered_citizens r
    JOIN hierarchy_cte h ON r.recruitment_id = h.department_id
    WHERE h.organization_id IS NOT NULL
    GROUP BY h.organization_id, recording_date

    UNION ALL

    -- Агрегируем для Region
    SELECT 
        h.region_id AS level_id, 
        SUM(r.reaching_17_age) AS total_reaching_17_age,
        SUM(r.women_military_specialty) AS total_women_military_specialty,
        SUM(r.count_all) AS total_count_all,
        recording_date AS recording_date, 
        'Region' AS level
    FROM forecast.registered_citizens r
    JOIN hierarchy_cte h ON r.recruitment_id = h.department_id
    WHERE h.region_id IS NOT NULL
    GROUP BY h.region_id, recording_date

    UNION ALL

    -- Агрегируем для Ministry
    SELECT 
        h.ministry_id AS level_id, 
        SUM(r.reaching_17_age) AS total_reaching_17_age,
        SUM(r.women_military_specialty) AS total_women_military_specialty,
        SUM(r.count_all) AS total_count_all,
        recording_date AS recording_date, 
        'Ministry' AS level
    FROM forecast.registered_citizens r
    JOIN hierarchy_cte h ON r.recruitment_id = h.department_id
    WHERE h.ministry_id IS NOT NULL
    GROUP BY h.ministry_id, recording_date
)
SELECT * FROM aggregated_counts;</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>672</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output</name>
    <type>TableOutput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>1000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
      <field>
        <column_name>recruitment_id</column_name>
        <stream_name>level_id</stream_name>
      </field>
      <field>
        <column_name>reaching_17_age</column_name>
        <stream_name>total_reaching_17_age</stream_name>
      </field>
      <field>
        <column_name>women_military_specialty</column_name>
        <stream_name>total_women_military_specialty</stream_name>
      </field>
      <field>
        <column_name>count_all</column_name>
        <stream_name>total_count_all</stream_name>
      </field>
      <field>
        <column_name>recording_date</column_name>
        <stream_name>recording_date</stream_name>
      </field>
      <field>
        <column_name>schema</column_name>
        <stream_name>level</stream_name>
      </field>
    </fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_field/>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_field/>
    <return_keys>N</return_keys>
    <schema>forecast</schema>
    <specify_fields>Y</specify_fields>
    <table>registered_citizens</table>
    <tablename_field/>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>912</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
