WITH DuplicateDebitur AS (
    SELECT KD_DEBITUR_SEQ
    FROM prm_debitur_addr pda
    GROUP BY pda.KD_DEBITUR_SEQ
    HAVING COUNT(KD_DEBITUR_SEQ) > 1
),
MinUrutSeq AS (
    SELECT pda.KD_DEBITUR_SEQ, MIN(TRIM(pda.URUT_SEQ)) AS Min_URUT_SEQ
    FROM prm_debitur_addr pda
    GROUP BY pda.KD_DEBITUR_SEQ
    HAVING COUNT(KD_DEBITUR_SEQ) > 1
)
SELECT pda.`KD_DEBITUR_SEQ` AS mig_kd_debitur_seq
       , pda.`URUT_SEQ` AS mig_urut_seq
       , pda.`ADDR_TYPE_SEQ` AS mig_addr_type_seq
       , pda.`ADDR_VALUE` AS mig_addr_value
       , pda.`USER_UPDATE` AS mig_user_update
       , pda.`TGL_UPDATE` AS mig_tgl_update
       , pda.`FLAG_OTO` AS mig_flag_oto
       , pda.`USER_OTO` AS mig_user_oto
       , pda.`TGL_OTO` AS mig_tgl_oto
       , pda.`KD_KEL_SEQ` AS mig_kd_kel_seq
       , pda.`ADDR_DISPLAY` AS mig_addr_display
       , pda.`KD_CABANG` AS mig_kd_cabang
       , pda.`NAMA_DAERAH` AS mig_nama_daerah
       , 1 AS is_mig
FROM prm_debitur_addr pda
JOIN DuplicateDebitur dd ON dd.KD_DEBITUR_SEQ = pda.KD_DEBITUR_SEQ
LEFT JOIN MinUrutSeq mus ON mus.KD_DEBITUR_SEQ = pda.KD_DEBITUR_SEQ
    AND mus.Min_URUT_SEQ = TRIM(pda.URUT_SEQ)
WHERE mus.Min_URUT_SEQ IS NULL
ORDER BY pda.KD_DEBITUR_SEQ;