<?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
fr_check AS (
    SELECT
        last_recruitment_id AS recruitment_id,
        recruitment_created_date,
        status,
        job_name
    FROM public.etl_checkpoints
    WHERE status = 'ERROR' AND job_name IN ('citizen_flow1', 'citizen_flow2', 'citizen_flow3', 'citizen_flow4', 'citizen_flow5')
),
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,
        ((rn - 1) % CEIL(total_rows / 5.0)) + 1 AS position
    FROM ordered
),
pivoted AS (
    SELECT
        position,
        MAX(CASE WHEN flow_num = 1 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow1') THEN created_at END) AS cr_flow1,
        MAX(CASE WHEN flow_num = 1 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow1') THEN idm_id::text END) AS idm_flow1,
        MAX(CASE WHEN flow_num = 2 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow2') THEN created_at END) AS cr_flow2,
        MAX(CASE WHEN flow_num = 2 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow2') THEN idm_id::text END) AS idm_flow2,
        MAX(CASE WHEN flow_num = 3 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow3') THEN created_at END) AS cr_flow3,
        MAX(CASE WHEN flow_num = 3 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow3') THEN idm_id::text END) AS idm_flow3,
        MAX(CASE WHEN flow_num = 4 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow4') THEN created_at END) AS cr_flow4,
        MAX(CASE WHEN flow_num = 4 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow4') THEN idm_id::text END) AS idm_flow4,
        MAX(CASE WHEN flow_num = 5 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow5') THEN created_at END) AS cr_flow5,
        MAX(CASE WHEN flow_num = 5 AND created_at >= (SELECT recruitment_created_date FROM fr_check WHERE job_name = 'citizen_flow5') THEN idm_id::text END) AS idm_flow5
    FROM splitted
    GROUP BY position
),
remains as (
    SELECT
    	position,
        cr_flow1,  
        idm_flow1::uuid,
        cr_flow2,  
        idm_flow2::uuid,
        cr_flow3,  
        idm_flow3::uuid,
        cr_flow4,  
        idm_flow4::uuid,
        cr_flow5,  
        idm_flow5::uuid
    FROM pivoted
    WHERE (cr_flow1 IS NOT NULL AND idm_flow1 IS NOT NULL)
       OR (cr_flow2 IS NOT NULL AND idm_flow2 IS NOT NULL)
       OR (cr_flow3 IS NOT NULL AND idm_flow3 IS NOT NULL)
       OR (cr_flow4 IS NOT NULL AND idm_flow4 IS NOT NULL)
       OR (cr_flow5 IS NOT NULL AND idm_flow5 IS NOT NULL)
    order by position
),
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>
