SELECT TARIH,DEPT as DeptKodu,ADI as DepartmanAdi,AVANSTUTAR as KasaAvansToplam,TAHSILAT as SatisTahsilatToplam,ODEME as KasaCikisOdemeler,DEVIRTUTAR as DevirCikisToplam,
AVANSTUTAR+TAHSILAT+ODEME+DEVIRTUTAR BakiyeToplam, FaturaToplam FROM
(
SELECT F.TARIH,F.DEPT,D.ADI,
ISNULL((SELECT SUM(GenelToplam) FROM EFATURA X WHERE  X.FaturaTarih=F.TARIH ),0) FaturaToplam,
ISNULL((SELECT SUM(BORC) FROM FOLYOISL X WHERE X.DEPT=F.DEPT AND X.TARIH=F.TARIH AND X.ENO='AVANS' AND D.ISLEMTURU=3 and d.kodu='90'),0)*(-1) AVANSTUTAR,
ISNULL((SELECT SUM(BORC) FROM FOLYOISL X WHERE X.DEPT=F.DEPT AND X.TARIH=F.TARIH AND X.ENO<>'AVANS' AND X.ENO<>'DEVIR' AND X.ENO<>'ODEME' AND D.ISLEMTURU=3  and d.KODU='90'),0)*(-1) TAHSILAT,
ISNULL((SELECT SUM(BORC) FROM FOLYOISL X WHERE X.DEPT=F.DEPT AND X.TARIH=F.TARIH AND X.ENO='ODEME' AND D.ISLEMTURU=3 and d.KODU='90'),0) ODEME,
ISNULL((SELECT SUM(BORC) FROM FOLYOISL X WHERE X.DEPT=F.DEPT AND X.TARIH=F.TARIH AND X.ENO='DEVIR' AND D.ISLEMTURU=3 and d.KODU='90'),0)*(-1) DEVIRTUTAR
FROM
(SELECT TARIH,DEPT FROM FOLYOISL
UNION
SELECT TARIH,DEPT FROM FOLYOARV
UNION
SELECT FaturaTarih,KASA FROM EFATURA
) F
LEFT JOIN DEPART D ON (F.DEPT=D.KODU)
WHERE F.DEPT IN (SELECT KODU FROM DEPART WHERE ISLEMTURU=3)
) X
WHERE X.DEPT='90'
order by TARIH ASC