WITH DuplicateDebitur AS (
    SELECT pdc.KD_DEBITUR_SEQ
    FROM prm_debitur_comm pdc
    GROUP BY pdc.KD_DEBITUR_SEQ
    HAVING COUNT(pdc.KD_DEBITUR_SEQ) > 1
),
MinUrutSeq AS (
    SELECT pdc.KD_DEBITUR_SEQ, MIN(TRIM(pdc.URUT_SEQ)) AS Min_URUT_SEQ
    FROM prm_debitur_comm pdc
    GROUP BY pdc.KD_DEBITUR_SEQ
    HAVING COUNT(pdc.KD_DEBITUR_SEQ) > 1
)
SELECT pdc.`KD_DEBITUR_SEQ` AS mig_kd_debitur_seq
       , pdc.`URUT_SEQ` AS mig_urut_seq
       , pdc.`COMM_TYPE_SEQ` AS mig_comm_type_seq
       , pdc.`COMM_VALUE` AS mig_comm_value
       , pdc.`ADDR_TYPE_SEQ` AS mig_addr_type_seq
       , pdc.`USER_UPDATE` AS mig_user_update
       , pdc.`TGL_UPDATE` AS mig_tgl_update
       , pdc.`FLAG_OTO` AS mig_flag_oto
       , pdc.`USER_OTO` AS mig_user_oto
       , pdc.`TGL_OTO` AS mig_tgl_oto
       , pdc.`COMM_DISPLAY` AS mig_comm_display
       , pdc.`KD_CABANG` AS mig_kd_cabang
       , 1 AS is_mig
FROM prm_debitur_comm pdc
JOIN DuplicateDebitur dd ON dd.KD_DEBITUR_SEQ = pdc.KD_DEBITUR_SEQ
LEFT JOIN MinUrutSeq mus ON mus.KD_DEBITUR_SEQ = pdc.KD_DEBITUR_SEQ
    AND mus.Min_URUT_SEQ = TRIM(pdc.URUT_SEQ)
WHERE mus.Min_URUT_SEQ IS NULL
ORDER BY pdc.KD_DEBITUR_SEQ;