with df_cf as (
select num_ruc_hash, num_telefono, prob, codmes_ejecucion, codmes_campana,
row_number() over( partition by codmes_ejecucion, codmes_campana, num_ruc_hash, num_telefono order by fec_proceso desc, prob desc) as orden
from d_mdl_vpc_disc.HM_UNIVERSO_SCORE_RUC_TEL_CF2
WHERE codmes_campana >= '202405'
),
df_sf as (
select num_ruc_hash, num_telefono, prob, codmes_ejecucion, codmes_campana,
row_number() over( partition by codmes_ejecucion, codmes_campana, num_ruc_hash, num_telefono order by fec_proceso desc, prob desc) as orden
from d_mdl_vpc_disc.HM_UNIVERSO_SCORE_RUC_TEL_SF2
WHERE codmes_campana >= '202405'
)
SELECT UU.num_ruc_hash, UU.num_telefono, UU.prob, UU.codmes_ejecucion, UU.codmes_campana,
row_number() over( partition by UU.codmes_ejecucion, UU.codmes_campana, UU.num_ruc_hash order by prob desc) as prioridad_telefono
FROM(
select *
from df_cf
where orden = 1
union
select *
from df_sf
where orden = 1
) UU
Add a code snippet to your website: www.paste.org