WITH CA_EVRAK AS (SELECT 'C' AS C_S,SC_VERENK, TUTAR, VADETRH FROM dbo.TBLMCEK WHERE (SC_SONDUR = 'B') AND (AS_C='A') AND (VADETRH > GETDATE()) UNION ALL SELECT 'S',SC_VERENK, TUTAR, VADETRH FROM dbo.TBLMSEN WHERE (SC_SONDUR = 'B') AND (AS_C='A') AND (VADETRH > GETDATE()) ), CC_EVRAK AS (SELECT 'C' AS C_S,SC_VERENK, TUTAR, VADETRH FROM dbo.TBLMCEK WHERE (SC_SONDUR = 'B') AND (AS_C='C') AND (VADETRH > GETDATE()) UNION ALL SELECT 'S',SC_VERENK, TUTAR, VADETRH FROM dbo.TBLMSEN WHERE (SC_SONDUR = 'B') AND (AS_C='C') AND (VADETRH > GETDATE()) ), SON_EVRAK AS (SELECT SC_VERENK,C_S,TUTAR,VADETRH SON_TARIH FROM (SELECT SC_VERENK,TUTAR,VADETRH,C_S,ROW_NUMBER() OVER (PARTITION BY SC_VERENK ORDER BY SC_VERENK,VADETRH DESC) SIRA FROM (SELECT * FROM CA_EVRAK UNION ALL SELECT * FROM CC_EVRAK) V ) W WHERE SIRA=1 ),
CABAKIYE AS (SELECT SC_VERENK,SUM(CASE WHEN C_S='C' THEN TUTAR ELSE 0 END) KENDI_CEKI_TOP, SUM(CASE WHEN C_S='S' THEN TUTAR ELSE 0 END) KENDI_SENEDI_TOP, CAST(AVG(CASE WHEN C_S='C' THEN CAST(VADETRH AS FLOAT) END) AS DATETIME) CEK_ORT_VD, CAST(AVG(CASE WHEN C_S='S' THEN CAST(VADETRH AS FLOAT) END) AS DATETIME) SEN_ORT_VD FROM CA_EVRAK GROUP BY SC_VERENK ), CCBAKIYE AS (SELECT SC_VERENK,SUM(CASE WHEN C_S='C' THEN TUTAR ELSE 0 END) CIRO_CEK_TOP, SUM(CASE WHEN C_S='S' THEN TUTAR ELSE 0 END) CIRO_SENEDI_TOP, CAST(AVG(CASE WHEN C_S='C' THEN CAST(VADETRH AS FLOAT) END) AS DATETIME) CEK_ORT_VD, CAST(AVG(CASE WHEN C_S='S' THEN CAST(VADETRH AS FLOAT) END) AS DATETIME) SEN_ORT_VD FROM CC_EVRAK GROUP BY SC_VERENK ), GENELBAKIYE AS (SELECT CARI_KOD,SUM(BORC - ALACAK) BAKIYE FROM TBLCAHAR WHERE CARI_KOD like '120-%' and CARI_KOD not like '120-99%' GROUP BY CARI_KOD HAVING SUM(BORC - ALACAK) <> 0) SELECT A.CARI_KOD,dbo.trk(CARI_ISIM)CARI_ISIM,dbo.trk(PLASIYER_ACIKLAMA) PLASIYER, B.KENDI_CEKI_TOP + B.KENDI_SENEDI_TOP KENDI_EVRAK_TOPLAMI, B.CEK_ORT_VD KC_ORTALAMA_VADE,B.SEN_ORT_VD KS_ORTALAMA_VADE, D.CIRO_CEK_TOP + D.CIRO_SENEDI_TOP CIRO_EVRAK_TOPLAMI, D.CEK_ORT_VD CC_ORTALAMA_VADE,D.SEN_ORT_VD CS_ORTALAMA_VADE,G.C_S,G.TUTAR,G.SON_TARIH, E.BAKIYE GENEL_BAKIYE FROM TBLCASABIT A LEFT JOIN CABAKIYE B ON A.CARI_KOD=B.SC_VERENK LEFT JOIN CCBAKIYE D ON A.CARI_KOD=D.SC_VERENK LEFT JOIN GENELBAKIYE E ON A.CARI_KOD=E.CARI_KOD LEFT JOIN TBLCARIPLASIYER F ON A.PLASIYER_KODU=F.PLASIYER_KODU LEFT JOIN SON_EVRAK G ON A.CARI_KOD=G.SC_VERENK WHERE A.CARI_KOD LIKE '120-%' AND A.CARI_KOD NOT LIKE '120-99%' AND A.PLASIYER_KODU NOT IN('AV','AVK','SC','INT') AND (E.BAKIYE IS NOT NULL OR C_S IS NOT NULL)
------------- Mehmet Baykan
|