<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>recruitment_rows_five_flow_citizen_spouse</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/04/18 09:48:01.970</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/04/18 09:48:01.970</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>832</xloc>
      <yloc>304</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,
        ((rn - 1) % CEIL(total_rows / 5.0)) + 1 AS position
    FROM ordered
),
pivoted AS (
    SELECT
        position,

        MAX(CASE WHEN flow_num = 1 THEN created_at END) AS cr_flow1,
        MAX(CASE WHEN flow_num = 1 THEN idm_id::text END) AS idm_flow1,

        MAX(CASE WHEN flow_num = 2 THEN created_at END) AS cr_flow2,
        MAX(CASE WHEN flow_num = 2 THEN idm_id::text END) AS idm_flow2,

        MAX(CASE WHEN flow_num = 3 THEN created_at END) AS cr_flow3,
        MAX(CASE WHEN flow_num = 3 THEN idm_id::text END) AS idm_flow3,

        MAX(CASE WHEN flow_num = 4 THEN created_at END) AS cr_flow4,
        MAX(CASE WHEN flow_num = 4 THEN idm_id::text END) AS idm_flow4,

        MAX(CASE WHEN flow_num = 5 THEN created_at END) AS cr_flow5,
        MAX(CASE WHEN flow_num = 5 THEN idm_id::text END) AS idm_flow5

    FROM splitted
    GROUP BY position
    ORDER BY position
)
SELECT
    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
limit 3</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>464</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
