with ranked as (
  select
    product_id,
    row_number() over (order by updated_at desc, created_at desc, product_id desc) as featured_rank
  from data_product_details
  where featured_position is not null
    and status <> 'deleted'
)
update data_product_details d
set featured_position = case when ranked.featured_rank = 1 then 1 else null end,
    updated_at = now()
from ranked
where d.product_id = ranked.product_id;
