--CREATE VIEW dbo.VCr_TahsilatVadeRaporu  AS  
SELECT TOP (100) PERCENT ROW_NUMBER() over (order by Odeme_1.Tarih ) ,Odeme_1.NoId,dbo.AMUSTERI.MUSNO AS CariNo, dbo.AMUSTERI.MUSKOD AS CariKodu, dbo.AMUSTERI.MUSADI AS CariAdi,   
 dbo.ACARI.BAACIKLA AS IslemTuru, Odeme_1.IslemNo, Odeme_1.Aciklama, dbo.ACARI.TARIH AS IslemTarihi, Odeme_1.Tarih AS VadeTarihi, CONVERT(int,   
 Odeme_1.Tarih) - CONVERT(int, dbo.ACARI.TARIH) AS VadeGunu, CONVERT(int,   (SELECT        YETKITAR   FROM            dbo.ASABIT)) -
 CONVERT(int, Odeme_1.Tarih) AS GecikenGunSayisi, Odeme_1.Borc AS HareketTutari, dbo.AMUSTERI.MUSBORC AS CariToplamBorc,   
 dbo.AMUSTERI.MUSALACAK AS CariToplamAlacak, dbo.AMUSTERI.MUSBAKIYE AS CariToplamBakiye, dbo.AMUSTERI.MUSBAKIYE + Odeme_1.Borc -  
 (SELECT        SUM(Borc) AS Expr1   FROM            dbo.Odeme  
 WHERE        (CariNo = Odeme_1.CariNo) AND (Tarih >= Odeme_1.Tarih) AND (IslemNo >= Odeme_1.IslemNo)) AS KalanTutari, dbo.STOKHARDETBAG.FatSerNo,   
                         dbo.STOKHARDETBAG.FaturaNo  
FROM            dbo.AMUSTERI INNER JOIN  
                         dbo.Odeme AS Odeme_1 ON dbo.AMUSTERI.MUSKOD = Odeme_1.CariKodu INNER JOIN  
                         dbo.ACARI ON Odeme_1.IslemNo = dbo.ACARI.ISLEMNO AND Odeme_1.CariKodu = dbo.ACARI.MUSKOD LEFT OUTER JOIN  
                         dbo.STOKHARDETBAG ON dbo.ACARI.ISLEMNO = dbo.STOKHARDETBAG.IslemNo  
WHERE        (Odeme_1.Borc > 0) AND (Odeme_1.Alacak = 0) AND (dbo.AMUSTERI.MUSBAKIYE + Odeme_1.Borc -  
                             (SELECT        SUM(Borc) AS Expr1  
                               FROM            dbo.Odeme AS Odeme_2  
                               WHERE        (CariNo = Odeme_1.CariNo) AND (Tarih >= Odeme_1.Tarih)) > 0)  AND AMUSTERI.MUSNO=391
ORDER BY CONVERT(int, Odeme_1.Tarih) DESC, Odeme_1.IslemNo ,Odeme_1.NoId 