
-- Please run only once

/*
select * from ydet,yres
where yres_id=ydet_yres_id 
and exists (select 1 from ylpr where ylpr_ydet_id=ydet_id)
and (   (    yres_contract_zcur_id=(select wuss_value from wuss where wuss_name='BaseCurrency')
         and ydet_ratediff=-(select sum(ylpr_amount) from ylpr where ylpr_ydet_id=ydet_id)
        )
     or (    yres_contract_zcur_id<>(select wuss_value from wuss where wuss_name='BaseCurrency')
        and ydet_foreignratediff=-(select sum(ylpr_foreignamount) from ylpr where ylpr_ydet_id=ydet_id)
        )
    )    
*/      

update ydet
set ydet_ratediff=null
   ,ydet_foreignratediff=null
where exists (select 1 from ylpr where ylpr_ydet_id=ydet_id)
and (   (    (select yres_contract_zcur_id from yres where yres_id=ydet_yres_id)=(select wuss_value from wuss where wuss_name='BaseCurrency')
         and ydet_ratediff=-(select sum(ylpr_amount) from ylpr where ylpr_ydet_id=ydet_id)
        )
     or (    (select yres_contract_zcur_id from yres where yres_id=ydet_yres_id)<>(select wuss_value from wuss where wuss_name='BaseCurrency')
        and ydet_foreignratediff=-(select sum(ylpr_foreignamount) from ylpr where ylpr_ydet_id=ydet_id)
        )
    )    
; 
 
