<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.age_pr1(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.age) 2</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update (total_registered.age) 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>registered</field>
        <name>registered</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>age</table>
      <value>
        <name>"17_years"</name>
        <rename>17_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"18-26_years"</name>
        <rename>18-26_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"27-30_years"</name>
        <rename>27-30_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"31-35_years"</name>
        <rename>31-35_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"36-40_years"</name>
        <rename>36-40_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"41-45_years"</name>
        <rename>41-45_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"51+_years"</name>
        <rename>41-45_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"all_M_W"</name>
        <rename>gender</rename>
        <update>N</update>
      </value>
      <value>
        <name>"46-50_years"</name>
        <rename>46-50_year_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"17_years_percent"</name>
        <rename>17_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"18-26_years_percent"</name>
        <rename>18-26_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"27-30_years_percent"</name>
        <rename>27-30_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"31-35_years_percent"</name>
        <rename>31-35_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"36-40_years_percent"</name>
        <rename>36-40_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"41-45_years_percent"</name>
        <rename>41-45_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"46-50_years_percent"</name>
        <rename>46-50_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"51+_years_percent"</name>
        <rename>51+_year_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>registered</name>
        <rename>registered</rename>
        <update>N</update>
      </value>
      <value>
        <name>total_count</name>
        <rename>total_count</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>1920</xloc>
      <yloc>720</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 (
    -- Преобразуем строковый массив в таблицу UUID
    SELECT unnest(string_to_array(trim(both '{}' from '${ARR_PR_1}'), ','))::uuid AS recruitment_id
),
/*
last_status_r 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_r 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
),
*/
last_status_n as (select recruit_id,
                     max(date_time) filter (where status in ('1', '12.7', '8.2', '8.3')) max_date_time
                     from recruits_history
                     where status in ('1', '12.7', '8.2', '8.3')
                     group by recruit_id
),
notregistered as (
                    select
                    distinct rh.recruit_id,
                    rh.id as recruit_history_id
                    from recruits_history rh
                    join last_status_n ls on rh.recruit_id=ls. recruit_id and rh.date_time=ls.max_date_time
                    where rh.status in ('1', '12.7', '8.2', '8.3') 
--and rh.source is not null
),
recruit_data AS (
    SELECT 
        r.id,
        ri.recruit_id AS recruit_id,
        r.current_recruitment_id AS recruitment_id,
        --coalesce (r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id,
        r.gender,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        'true' AS registered,
        r.birth_date
    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)
/*
        r.id in (SELECT recruit_id FROM registered) and
        rh.id in (SELECT recruit_history_id FROM registered)
        and coalesce(r.current_recruitment_id,rh.current_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
*/
    
    UNION ALL 

    SELECT 
        r.id,
        ri.recruit_id AS recruit_id,
        coalesce (r.target_recruitment_id, rh.target_recruitment_id) AS recruitment_id,
        r.gender,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        'false' AS registered,
        r.birth_date
    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.id in (SELECT recruit_id FROM notregistered) and
        rh.id in (SELECT recruit_history_id FROM notregistered)
        and coalesce(r.target_recruitment_id,rh.target_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
         AND r.current_recruitment_id is null

    UNION ALL 

    SELECT 
        r.id,
        ri.recruit_id AS recruit_id,
        coalesce (r.target_recruitment_id, rh.target_recruitment_id) AS recruitment_id,
        r.gender,
        'ALL' AS convicts,
        'false' AS registered,
        r.birth_date
    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.id in (SELECT recruit_id FROM notregistered) and
        rh.id in (SELECT recruit_history_id FROM notregistered)
        and coalesce(r.target_recruitment_id,rh.target_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
     AND r.current_recruitment_id is null

    UNION ALL 

    SELECT 
        r.id,
        ri.recruit_id AS recruit_id,
        r.current_recruitment_id AS recruitment_id,
        --coalesce (r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id,
        r.gender,
        'ALL' AS convicts,
        'true' AS registered,
        r.birth_date
    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)
/*
        r.id in (SELECT recruit_id FROM registered) and
        rh.id in (SELECT recruit_history_id FROM registered)
        and coalesce(r.current_recruitment_id,rh.current_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
*/
    
),
total_count AS (
    SELECT 
        rd.recruitment_id,
        rd.gender,
        rd.registered,
        rd.convicts,
        COUNT(distinct rd.recruit_id) AS total
    FROM recruit_data rd 
    GROUP BY rd.recruitment_id, rd.gender, rd.registered, rd.convicts
),
total_count_all AS (
    SELECT 
        rd.recruitment_id,
        rd.registered,
        rd.convicts,
        COUNT(distinct rd.recruit_id) AS total
    FROM recruit_data rd 
    GROUP BY rd.recruitment_id, rd.registered, rd.convicts
),
age_counts AS (
    SELECT 
        rd.recruitment_id,
        rd.gender,
        rd.registered,
        rd.convicts,
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) = 17) AS "17_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) BETWEEN 18 AND 26) AS "18-26_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) BETWEEN 27 AND 30) AS "27-30_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) BETWEEN 31 AND 35) AS "31-35_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) BETWEEN 36 AND 40) AS "36-40_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) BETWEEN 41 AND 45) AS "41-45_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) BETWEEN 46 AND 50) AS "46-50_year_count",
        COUNT(distinct rd.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(rd.birth_date)) >= 51) AS "51+_year_count"
    FROM recruit_data rd 
    GROUP BY rd.recruitment_id, rd.gender, rd.registered, rd.convicts
),
age_counts_all AS (
    SELECT 
        r.recruitment_id,
        r.registered,
        r.convicts,
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) = 17) AS "17_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) BETWEEN 18 AND 26) AS "18-26_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) BETWEEN 27 AND 30) AS "27-30_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) BETWEEN 31 AND 35) AS "31-35_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) BETWEEN 36 AND 40) AS "36-40_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) BETWEEN 41 AND 45) AS "41-45_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) BETWEEN 46 AND 50) AS "46-50_year_count",
        COUNT(distinct r.recruit_id) FILTER (WHERE EXTRACT(YEAR FROM AGE(r.birth_date)) >= 51) AS "51+_year_count"
    FROM recruit_data r
    GROUP BY r.recruitment_id, r.registered, r.convicts
)
SELECT
    current_date AS recording_date,
    COALESCE(ac."17_year_count", 0) AS "17_year_count",
    COALESCE(ROUND((COALESCE(ac."17_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "17_year_percent",
    COALESCE(ac."18-26_year_count", 0) AS "18-26_year_count",
    COALESCE(ROUND((COALESCE(ac."18-26_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "18-26_year_percent",
    COALESCE(ac."27-30_year_count", 0) AS "27-30_year_count",
    COALESCE(ROUND((COALESCE(ac."27-30_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "27-30_year_percent",
    COALESCE(ac."31-35_year_count", 0) AS "31-35_year_count",
    COALESCE(ROUND((COALESCE(ac."31-35_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "31-35_year_percent",
    COALESCE(ac."36-40_year_count", 0) AS "36-40_year_count",
    COALESCE(ROUND((COALESCE(ac."36-40_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "36-40_year_percent",
    COALESCE(ac."41-45_year_count", 0) AS "41-45_year_count",
    COALESCE(ROUND((COALESCE(ac."41-45_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "41-45_year_percent",
    COALESCE(ac."46-50_year_count", 0) AS "46-50_year_count",
    COALESCE(ROUND((COALESCE(ac."46-50_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "46-50_year_percent",
    COALESCE(ac."51+_year_count", 0) AS "51+_year_count",
    COALESCE(ROUND((COALESCE(ac."51+_year_count", 0) * 100.0) / NULLIF(tc.total, 0), 2), 0) AS "51+_year_percent",
    COALESCE(tc.total, 0) AS total_count,
    tc.recruitment_id,
    (CASE
            WHEN tc.gender = 'MALE' THEN 'M'
            ELSE 'W'
    END)::text AS gender,
    tc.convicts,
    tc.registered::text
FROM total_count tc
LEFT JOIN age_counts ac
    ON ac.recruitment_id = tc.recruitment_id
    AND ac.gender = tc.gender
    AND ac.registered = tc.registered
    AND ac.convicts =  tc.convicts
UNION ALL
SELECT
    current_date AS recording_date,
    COALESCE(aca."17_year_count", 0) AS "17_year_count",
    COALESCE(ROUND((COALESCE(aca."17_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "17_year_percent",
    COALESCE(aca."18-26_year_count", 0) AS "18-26_year_count",
    COALESCE(ROUND((COALESCE(aca."18-26_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "18-26_year_percent",
    COALESCE(aca."27-30_year_count", 0) AS "27-30_year_count",
    COALESCE(ROUND((COALESCE(aca."27-30_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "27-30_year_percent",
    COALESCE(aca."31-35_year_count", 0) AS "31-35_year_count",
    COALESCE(ROUND((COALESCE(aca."31-35_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "31-35_year_percent",
    COALESCE(aca."36-40_year_count", 0) AS "36-40_year_count",
    COALESCE(ROUND((COALESCE(aca."36-40_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "36-40_year_percent",
    COALESCE(aca."41-45_year_count", 0) AS "41-45_year_count",
    COALESCE(ROUND((COALESCE(aca."41-45_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "41-45_year_percent",
    COALESCE(aca."46-50_year_count", 0) AS "46-50_year_count",
    COALESCE(ROUND((COALESCE(aca."46-50_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "46-50_year_percent",
    COALESCE(aca."51+_year_count", 0) AS "51+_year_count",
    COALESCE(ROUND((COALESCE(aca."51+_year_count", 0) * 100.0) / NULLIF(tca.total, 0), 2), 0) AS "51+_year_percent",
    COALESCE(tca.total, 0) AS total_count,
    tca.recruitment_id,
    'ALL' AS gender,
    tca.convicts,
    tca.registered::text
FROM total_count_all tca
LEFT JOIN age_counts_all aca
    ON aca.recruitment_id = tca.recruitment_id
    AND aca.registered = tca.registered
    AND aca.convicts =  tca.convicts</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>1296</xloc>
      <yloc>720</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
