<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.marital_status_pr2(m_c)</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <pipeline_status>0</pipeline_status>
    <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>2024/08/02 11:56:22.507</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/08/02 11:56:22.507</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input (person_registry) регионы/мужчины</from>
      <to>Insert / update (total_registered.marital_status)  2</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update (total_registered.marital_status)  2</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>
      <key>
        <condition>=</condition>
        <field>"all_M_W"</field>
        <name>gender</name>
      </key>
      <key>
        <condition>=</condition>
        <field>convicts</field>
        <name>convicts</name>
      </key>
      <schema>total_registered</schema>
      <table>marital_status</table>
      <value>
        <name>married</name>
        <rename>married</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_married</name>
        <rename>not_married</rename>
        <update>Y</update>
      </value>
      <value>
        <name>married_percent</name>
        <rename>married_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_married_percent</name>
        <rename>not_married_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"all_M_W"</name>
        <rename>gender</rename>
        <update>N</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>marital_status</name>
        <rename>total</rename>
        <update>Y</update>
      </value>
      <value>
        <name>other</name>
        <rename>other</rename>
        <update>Y</update>
      </value>
      <value>
        <name>other_percent</name>
        <rename>other_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>convicts</name>
        <rename>convicts</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1680</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input (person_registry) регионы/мужчины</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu_person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH 
ids AS (
    SELECT unnest(string_to_array(trim(both '{}' from '${ARR_PR_2}'), ','))::uuid AS recruitment_id
),
/*
last_status as (select recruit_id,
                     max(date_time) filter (where status not in ('1', '1.3', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '13')) max_date_time
                     from recruits_history
                     where status not in ('1', '1.3', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '13')
                     group by recruit_id
),
registered as (
                    select
                    distinct rh.recruit_id,
                    rh.id as recruit_history_id
                    from recruits_history rh
                    join last_status ls on rh.recruit_id=ls. recruit_id and rh.date_time=ls.max_date_time
                    where rh.status not in ('1', '1.3', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '13') 
                    --and rh.source is not null
),
*/
recruit_data AS (
    SELECT 
        ri.recruit_id,
        ri.info->'svedFL'->'svedSemPolozh'->>'semPolozh' AS sem_pol,
        r.gender,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        r.current_recruitment_id AS recruitment_id
        --coalesce (r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id    
    FROM public.recruits AS r
    JOIN public.recruits_info AS ri ON ri.recruit_id = r.id
    --JOIN public.recruits_history rh on rh.recruit_id=r.id
    WHERE r.current_recruitment_id::uuid IN (SELECT recruitment_id FROM ids)
        AND r.gender IN ('MALE', 'FEMALE')
/*
        AND r.id in (SELECT recruit_id FROM registered) 
        and rh.id in (SELECT recruit_history_id FROM registered)
        --and r.vu_current_info->>'recruitmentCommercialInfo' = 'true'
        and coalesce(r.current_recruitment_id,rh.current_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
*/

    UNION ALL 

    SELECT 
        ri.recruit_id,
        ri.info->'svedFL'-&gt;'svedSemPolozh'->>'semPolozh' AS sem_pol,
        r.gender,
        'ALL' AS convicts,
        r.current_recruitment_id AS recruitment_id
        --coalesce (r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id    
    FROM public.recruits AS r
    JOIN public.recruits_info AS ri ON ri.recruit_id = r.id
    --JOIN public.recruits_history rh on rh.recruit_id=r.id
    WHERE r.current_recruitment_id::uuid IN (SELECT recruitment_id FROM ids)
        AND r.gender IN ('MALE', 'FEMALE')
/*
        AND r.id in (SELECT recruit_id FROM registered) 
        and rh.id in (SELECT recruit_history_id FROM registered)
        --and r.vu_current_info->>'recruitmentCommercialInfo' = 'true'
        and coalesce(r.current_recruitment_id,rh.current_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
*/
),
married_counts AS (
    -- Считаем значения для каждого гендера
    SELECT 
        rd.recruitment_id,
        rd.gender,
        rd.convicts,
        COUNT(rd.recruit_id) FILTER (WHERE rd.sem_pol IN ('2', '3')) AS not_married, -- в расзводе/вдова/вдовец
        COUNT(rd.recruit_id) FILTER (WHERE rd.sem_pol = '1') AS married, -- статус в браке
        COUNT(rd.recruit_id) FILTER (WHERE rd.sem_pol IS NULL) AS other,
        COUNT(rd.recruit_id) AS total
    FROM recruit_data rd
    GROUP BY rd.recruitment_id, rd.gender, rd.convicts -- изменено 12.03.2025
),
all_counts AS (
    -- Считаем значения для ALL, суммируя данные по всем гендерам
    SELECT 
        recruitment_id,
        'ALL' AS gender,
        convicts,
        SUM(not_married) AS not_married,
        SUM(married) AS married,
        SUM(other) AS other,
        SUM(total) AS total
    FROM married_counts
    GROUP BY recruitment_id, convicts
)
-- Финальное объединение
SELECT 
    mc.not_married,
    mc.married,
    mc.total,
    mc.other,
    mc.convicts,
    COALESCE(ROUND(mc.not_married * 100.0 / NULLIF(mc.total, 0), 2), 0) AS not_married_percent,
    COALESCE(ROUND(mc.married * 100.0 / NULLIF(mc.total, 0), 2), 0) AS married_percent,
    COALESCE(ROUND(mc.other * 100.0 / NULLIF(mc.total, 0), 2), 0) AS other_percent,
    CASE 
        WHEN mc.gender = 'MALE' THEN 'M'
        ELSE 'W'
    END AS gender,
    current_date AS recording_date,
    mc.recruitment_id
FROM married_counts mc
UNION ALL
SELECT 
    ac.not_married,
    ac.married,
    ac.total,
    ac.other,
    ac.convicts,
    COALESCE(ROUND(ac.not_married * 100.0 / NULLIF(ac.total, 0), 2), 0) AS not_married_percent,
    COALESCE(ROUND(ac.married * 100.0 / NULLIF(ac.total, 0), 2), 0) AS married_percent,
    COALESCE(ROUND(ac.other * 100.0 / NULLIF(ac.total, 0), 2), 0) AS other_percent,
    ac.gender,
    current_date AS recording_date,
    ac.recruitment_id
FROM all_counts ac</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>1040</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
