SELECT YEAR(R.TARIH) AS YIL , MONTH(R.TARIH) AS AY,R.TARIH,R.ID AS FisNo, R.OdaNo, R.ADSOYADI AS AdiSoyadi, R.RGTAR AS Giris , R.RCITAR AS Cikis, R.GECE Gece, R.KKKALACAK AS Kisi, 
 SUM(CASE WHEN D.TUR = 'A' THEN F.BORC ELSE 0 END)*(-1) AS KasaAlacakDeptToplami,
 SUM(CASE WHEN D.TUR = 'B' THEN F.BORC ELSE 0 END) AS SatisDepartmanToplami,
 ISNULL(SUM(F.BORC),0) AS FolyoBakiyeToplam
FROM REZERVAS R
LEFT JOIN FOLYOISL F ON F.FOLYONO=R.ID
LEFT JOIN DEPART D ON F.DEPT=D.KODU 
WHERE R.DURUM='C'
GROUP BY YEAR(R.TARIH), MONTH(R.TARIH),R.TARIH,R.ODANO, R.ID,R.ADSOYADI,R.RGTAR,R.RCITAR,R.KKKALACAK,R.GECE,F.BORC
 
