update t_ai_daptracker 
set fsourcebillid = (
select a.fid from t_fa_card_fin a inner join t_fa_card_fin c 
on a.frealcardid = c.frealcardid where c.fid= t_ai_daptracker.fsourcebillid
and a.fendperiodid =99999999999
) where FBILLTYPE='fa_card_fin' and fsourcebillid in( select f.fid from t_fa_card_fin f inner join t_fa_card_real f1 on f.frealcardid = f1.fid 
 where f.fendperiodid !=99999999999 and f1.fmasterid in (
 select r.fmasterid from t_fa_card_real r group by r.fmasterid having count(*)=1
 ));

update t_ai_bizvoucher 
set fsourcebillid = (
select a.fid from t_fa_card_fin a inner join t_fa_card_fin c 
on a.frealcardid = c.frealcardid where c.fid= t_ai_bizvoucher.fsourcebillid
and a.fendperiodid =99999999999
) where fsourcebill='fa_card_fin' and fsourcebillid in( select f.fid from t_fa_card_fin f inner join t_fa_card_real f1 on f.frealcardid = f1.fid 
 where f.fendperiodid !=99999999999 and f1.fmasterid in (
 select r.fmasterid from t_fa_card_real r group by r.fmasterid having count(*)=1
 ));


update t_ai_daptracker   set fsourcebillid = 
(
	select a.fid from t_fa_card_fin a
	 inner join t_fa_card_real b on a.frealcardid = b.fid 
	 inner join t_fa_card_real d on d.fmasterid=b.fmasterid 
	 inner join t_fa_card_fin c on d.fid = c.frealcardid 
		where c.fid= t_ai_daptracker.fsourcebillid  and a.fendperiodid =99999999999  
) 
 where FBILLTYPE='fa_card_fin' and fsourcebillid in( select f.fid from t_fa_card_fin f inner join t_fa_card_real f1 on f.frealcardid = f1.fid 
 where f.fendperiodid !=99999999999 and f1.fmasterid in (
 select r.fmasterid from t_fa_card_real r  where r.FMASTERID!=0  group by r.fmasterid having count(*)>1
 )
);



update t_ai_bizvoucher set fsourcebillid = (
	select a.fid from t_fa_card_fin a
	 inner join t_fa_card_real b on a.frealcardid = b.fid 
	 inner join t_fa_card_real d on d.fmasterid=b.fmasterid 
	 inner join t_fa_card_fin c on d.fid = c.frealcardid 
where c.fid= t_ai_bizvoucher.fsourcebillid and a.fendperiodid =99999999999
) where fsourcebill='fa_card_fin' and fsourcebillid in( select f.fid from t_fa_card_fin f inner join t_fa_card_real f1 on f.frealcardid = f1.fid 
 where f.fendperiodid !=99999999999 and f1.fmasterid in (
 select r.fmasterid from t_fa_card_real r  where r.FMASTERID!=0  group by r.fmasterid having count(*)>1
 ));
 