<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>recruitment_rows_five_flow_if_error</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/05/23 14:28:22.572</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/05/23 14:28:22.572</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Copy rows to result</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Copy rows to result</name>
    <type>RowsToResult</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>880</xloc>
      <yloc>288</yloc>
    </GUI>
  </transform>
  <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 ordered AS (
    SELECT 
        idm_id,
        created_at,
        ROW_NUMBER() OVER (ORDER BY created_at) AS rn,
        COUNT(*) OVER () AS total_rows
    FROM ervu_dashboard.recruitment
),
splitted AS (
    SELECT *,
        CEIL(total_rows / 5.0) AS part_size,
        FLOOR((rn - 1) / CEIL(total_rows / 5.0)) + 1 AS flow_num
    FROM ordered
),
flow_ranges AS (
    SELECT 
        flow_num,
        MIN(created_at) AS min_date,
        MAX(created_at) AS max_date
    FROM splitted
    GROUP BY flow_num
),
recr_inf AS (
    SELECT DISTINCT COALESCE(cit.recruitment_id, cit.target_recruitment_id) AS idm_id
    FROM ervu_dashboard.citizen cit
	join ervu_dashboard.punishment ch on ch.recruit_id = cit.recruit_id
	
),
flow_data AS (
    SELECT 
        fr.flow_num,
        ri.idm_id,
        (SELECT r.created_at FROM ervu_dashboard.recruitment r 
         WHERE r.idm_id = ri.idm_id 
         AND r.created_at BETWEEN fr.min_date AND fr.max_date
         LIMIT 1) AS created_at
    FROM recr_inf ri
    CROSS JOIN flow_ranges fr
    WHERE EXISTS (
        SELECT 1 FROM ervu_dashboard.recruitment r 
        WHERE r.idm_id = ri.idm_id
    )
),
result_ma as (
    SELECT 
        MAX(created_at) FILTER(WHERE flow_num = 1) AS cr_flow1,
        (SELECT idm_id FROM flow_data WHERE flow_num = 1 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow1,
        
        MAX(created_at) FILTER(WHERE flow_num = 2) AS cr_flow2,
        (SELECT idm_id FROM flow_data WHERE flow_num = 2 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow2,
        
        MAX(created_at) FILTER(WHERE flow_num = 3) AS cr_flow3,
        (SELECT idm_id FROM flow_data WHERE flow_num = 3 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow3,
        
        MAX(created_at) FILTER(WHERE flow_num = 4) AS cr_flow4,
        (SELECT idm_id FROM flow_data WHERE flow_num = 4 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow4,
        
        MAX(created_at) FILTER(WHERE flow_num = 5) AS cr_flow5,
        (SELECT idm_id FROM flow_data WHERE flow_num = 5 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow5
    FROM flow_data
),
result_max as ( --костыль
    SELECT 
        CASE 
            WHEN cr_flow1 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow1
        END AS cr_flow1,
        idm_flow1,        
        CASE 
            WHEN cr_flow2 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow2
        END AS cr_flow2,
        idm_flow2,        
        CASE 
            WHEN cr_flow3 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow3
        END AS cr_flow3,
        idm_flow3,        
        CASE 
            WHEN cr_flow4 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow4
        END AS cr_flow4,
        idm_flow4,       
        CASE 
            WHEN cr_flow5 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow5
        END AS cr_flow5,
        idm_flow5
    FROM result_ma
),
remains as (
    select
        CASE
            WHEN created_at >= (select cr_flow1 from result_max) and flow_num = 1 THEN created_at
            ELSE null
        END as cr_flow1,
        CASE
            WHEN created_at >= (select cr_flow1 from result_max) and flow_num = 1 THEN idm_id
            ELSE null
        END as idm_flow1,

        CASE
            WHEN created_at >= (select cr_flow2 from result_max) and flow_num = 2 THEN created_at
            ELSE null
        END as cr_flow2,
        CASE
            WHEN created_at >= (select cr_flow2 from result_max) and flow_num = 2 THEN idm_id
            ELSE null
        END as idm_flow2,
        
        CASE
            WHEN created_at >= (select cr_flow3 from result_max) and flow_num = 3 THEN created_at
            ELSE null
        END as cr_flow3,
        CASE
            WHEN created_at >= (select cr_flow3 from result_max) and flow_num = 3 THEN idm_id
            ELSE null
        END as idm_flow3,
        
        CASE
            WHEN created_at >= (select cr_flow4 from result_max) and flow_num = 4 THEN created_at
            ELSE null
        END as cr_flow4,
        CASE
            WHEN created_at >= (select cr_flow4 from result_max) and flow_num = 4 THEN idm_id
            ELSE null
        END as idm_flow4,
        
        CASE
            WHEN created_at >= (select cr_flow5 from result_max) and flow_num = 5 THEN created_at
            ELSE null
        END as cr_flow5,
        CASE
            WHEN created_at >= (select cr_flow5 from result_max) and flow_num = 5 THEN idm_id
            ELSE null
        END as idm_flow5
    from splitted
),
idm1 as (
    select
        cr_flow1,
        idm_flow1
    from remains
    where cr_flow1 is not null and idm_flow1 is not null 
),
idm2 as (
    select
        cr_flow2,
        idm_flow2
    from remains
    where cr_flow2 is not null and idm_flow2 is not null 
),
idm3 as (
    select
        cr_flow3,
        idm_flow3
    from remains
    where cr_flow3 is not null and idm_flow3 is not null 
),
idm4 as (
    select
        cr_flow4,
        idm_flow4
    from remains
    where cr_flow4 is not null and idm_flow4 is not null 
),
idm5 as (
    select
        cr_flow5,
        idm_flow5
    from remains
    where cr_flow5 is not null and idm_flow5 is not null 
)
select 
    i1.*,
    i2.*,
    i3.*,
    i4.*,
    i5.*
FROM 
    (SELECT *, ROW_NUMBER() OVER () as rn FROM idm1) i1
FULL OUTER JOIN 
    (SELECT *, ROW_NUMBER() OVER () as rn FROM idm2) i2 ON i1.rn = i2.rn
FULL OUTER JOIN 
    (SELECT *, ROW_NUMBER() OVER () as rn FROM idm3) i3 ON i1.rn = i3.rn
FULL OUTER JOIN 
    (SELECT *, ROW_NUMBER() OVER () as rn FROM idm4) i4 ON i1.rn = i4.rn
FULL OUTER JOIN 
    (SELECT *, ROW_NUMBER() OVER () as rn FROM idm5) i5 ON i1.rn = i5.rn
ORDER BY COALESCE(i1.rn, i2.rn, i3.rn, i4.rn, i5.rn)
limit 2</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>512</xloc>
      <yloc>288</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
