SELECT    MUSNO, MUSKOD, MUSADI, MUSBORC, MUSALACAK, MUSBAKIYE,SaticiKodu,SaticiAdi,MUSIL,MUSILCE,OZELKOD1,OZELKOD2,OZELKOD3,OZELKOD4,
MUSADRES1,MUSADRES2,MUSTEL,Tipi,
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD ),0) as [ETOBuGunTamami],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih<= getDate()),0) as [ETOBuGun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-7 and Tarih < getDate()), 0) as [ETO7Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-14 and Tarih < getDate()-7), 0) as [ETO14Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-21 and Tarih < getDate()-14), 0) as [ETO21Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-30 and Tarih < getDate()-21), 0) as [ETO30Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-45 and Tarih < getDate()-30),0) as [ETO45Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-60 and Tarih < getDate()-45),0) as [ETO60Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-75 and Tarih < getDate()-60),0) as [ETO75Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-90 and Tarih < getDate()-75),0) as [ETO90Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-120 and Tarih < getDate()-90),0) as [ETO120Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-150 and Tarih < getDate()-120),0) as [ETO150Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-180 and Tarih < getDate()-150),0) as [ETO180Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-210 and Tarih < getDate()-180),0) as [ETO210Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-240 and Tarih < getDate()-210),0) as [ETO240Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-270 and Tarih < getDate()-240),0) as [ETO270Gun],
ISNULL((select sum(Borc) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih> getdate()-360 and Tarih < getDate()-270),0) as [ETO360Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih>= getDate()),0) as [GTOBuGun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+7 and Tarih > getDate()),0) as [GTO7Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+14 and Tarih > getDate()+7),0) as [GTO14Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+21 and Tarih > getDate()+14),0) as [GTO21Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+30 and Tarih > getDate()+21),0) as [GTO30Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+45 and Tarih > getDate()+30),0) as [GTO45Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+60 and Tarih > getDate()+45),0) as [GTO60Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+75 and Tarih > getDate()+60),0) as [GTO75Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+90 and Tarih > getDate()+75),0) as [GTO90Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+120 and Tarih > getDate()+90),0) as [GTO120Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+150 and Tarih > getDate()+120),0) as [GTO150Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+180 and Tarih > getDate()+150),0) as [GTO180Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+210 and Tarih > getDate()+180),0) as [GTO210Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+240 and Tarih > getDate()+210),0) as [GTO240Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+270 and Tarih > getDate()+240),0) as [GTO270Gun],
ISNULL((select sum(Borc)-sum(Alacak) from Odeme where CariNo = MUSNO and CariKodu = MUSKOD and Tarih< getdate()+360 and Tarih > getDate()+270),0) as [GTO360Gun] 
FROM dbo.V_AMUSTERI 
where MUSDURUM='A' and MUSBAKIYE > 0.01 
--and MUSNO = 28
order by dbo.V_AMUSTERI.MUSADI