 create or alter procedure sp_cekOrtalamaVadeHesapla(@NoID nvarchar(max))
 as
 declare @sqlText nvarchar(max)

 set @sqlText=' declare @toplamTutar float '+
	' select  @toplamTutar=sum(TUTAR) from CEK where NOID in ('+@NoID+') '+ 
	
	' Select @toplamTutar as cek_toplam, round(Sum(ortalama),0) as ortalama_vade_gun, '+
	' dateadd(day,round(Sum(ortalama),0),getdate()) ortalama_vade_tarih '+
	' from ( '+
	' select  NOID,VADETARIH,TUTAR,VadeGun,(VadeGun*TUTAR)/@toplamTutar as ortalama '+
	' from '+
	' (select  NOID,VADETARIH,TUTAR,datediff(day,getdate(),VADETARIH) as VadeGun '+
	' from CEK where NOID in ('+@NoID+') )T )T1 ';

exec(@sqlText)

 
 


 