<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>main_profile</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/06/17 10:17:01.438</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/06/17 10:17:01.438</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input main profile</from>
      <to>Table output</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input main profile</from>
      <to>Value mapper main profile</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Value mapper main profile</from>
      <to>Insert / update main profile</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update main profile</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>100</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruitment_id</field>
        <name>recruitment_id</name>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
      </key>
      <schema>appeals</schema>
      <table>main_profile</table>
      <value>
        <name>age</name>
        <rename>avg_age</rename>
        <update>Y</update>
      </value>
      <value>
        <name>child_min_18</name>
        <rename>avg_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>education</name>
        <rename>most_common_education_group</rename>
        <update>Y</update>
      </value>
      <value>
        <name>employment</name>
        <rename>most_common_employment</rename>
        <update>Y</update>
      </value>
      <value>
        <name>gender</name>
        <rename>most_common_gender</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recording_date</name>
        <rename>recording_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>schema</name>
        <rename>schema</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>800</xloc>
      <yloc>256</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input main profile</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>SELECT 
    c.recruitment_id,
    ROUND(AVG(DATE_PART('year', AGE(CURRENT_DATE, c.birth_date)))::numeric, 0) AS avg_age,
    MODE() WITHIN GROUP (ORDER BY c.gender) AS most_common_gender,
    MODE() WITHIN GROUP (ORDER BY c.employment) AS most_common_employment,
    ROUND(AVG(COALESCE(child_counts.child_count, 0))::numeric, 0) AS avg_children,
    MODE() WITHIN GROUP (ORDER BY education_groups.highest_group) AS most_common_education_group,
    current_date recording_date,
    'Department' as schema
FROM citizen c
LEFT JOIN (
    SELECT 
        recruit_id,
        COUNT(*) AS child_count
    FROM citizen_child
    WHERE death_date IS NULL 
      AND DATE_PART('year', AGE(CURRENT_DATE, birth_date)) &lt; 18
    GROUP BY recruit_id
) child_counts ON c.recruit_id = child_counts.recruit_id
LEFT JOIN (
    SELECT
        recruit_id,
        MIN(CASE 
            WHEN education_level_code IN ('1', '2', '3', '4', '5') THEN 1  -- Высшее
            WHEN education_level_code IN ('10', '11') THEN 2              -- Среднее проф.
            WHEN education_level_code IN ('7', '9') THEN 3                -- Общее
            ELSE 4                                                        -- Прочее/неизвестное
        END) AS highest_group
    FROM citizen_foreign_education_diploma
    GROUP BY recruit_id
) education_groups ON c.recruit_id = education_groups.recruit_id
join appeals.appeals_list al on c.system_esia_id = al.system_esia_id
GROUP BY c.recruitment_id
HAVING c.recruitment_id IS NOT NULL;</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>304</xloc>
      <yloc>256</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>gender</column_name>
        <stream_name>most_common_gender</stream_name>
      </field>
      <field>
        <column_name>age</column_name>
        <stream_name>avg_age</stream_name>
      </field>
      <field>
        <column_name>education</column_name>
        <stream_name>most_common_education_group</stream_name>
      </field>
      <field>
        <column_name>employment</column_name>
        <stream_name>most_common_employment</stream_name>
      </field>
      <field>
        <column_name>child_min_18</column_name>
        <stream_name>avg_children</stream_name>
      </field>
      <field>
        <column_name>schema</column_name>
        <stream_name>schema</stream_name>
      </field>
      <field>
        <column_name>recruitment_id</column_name>
        <stream_name>recruitment_id</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_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>appeals</schema>
    <specify_fields>Y</specify_fields>
    <table>main_profile</table>
    <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>560</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Value mapper main profile</name>
    <type>ValueMapper</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <field_to_use>most_common_education_group</field_to_use>
    <fields>
      <field>
        <source_value>1</source_value>
        <target_value>Высшее</target_value>
      </field>
      <field>
        <source_value>2</source_value>
        <target_value>Среднее проф.</target_value>
      </field>
      <field>
        <source_value>3</source_value>
        <target_value>Общее</target_value>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>528</xloc>
      <yloc>256</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
