SELECT YEAR(SH.TARIH) AS YIL,MONTH(SH.TARIH) AS AY,DEPO.ADI AS DEPOADI,DEPO.KODU AS DEPOKODU,SH.STOKKODU,ST.SFIYAT,ST.STOKADI,
CONVERT(DECIMAL(10,3),SUM(CASE WHEN SH.GDEPO=DEPO.KODU THEN SH.ADET ELSE 0 END)) GIRENADET,
CONVERT(DECIMAL(10,2),SUM(CASE WHEN SH.GDEPO=DEPO.KODU THEN SH.TUTAR ELSE 0 END)) GIRENTUTAR,
CONVERT(DECIMAL(10,3),SUM(CASE WHEN SH.CDEPO=DEPO.KODU THEN SH.ADET ELSE 0 END)) CIKANADET,
CONVERT(DECIMAL(10,2),SUM(CASE WHEN SH.CDEPO=DEPO.KODU THEN SH.TUTAR ELSE 0 END)) CIKANTUTAR,
CONVERT(DECIMAL(10,3),SUM(CASE WHEN SH.GDEPO=DEPO.KODU THEN SH.ADET ELSE 0 END)-SUM(CASE WHEN SH.CDEPO=DEPO.KODU THEN SH.ADET ELSE 0 END)) AS KALANADET,
CONVERT(DECIMAL(10,2),(ISNULL(SUM(CASE WHEN SH.GDEPO=DEPO.KODU THEN SH.ADET ELSE 0 END),0)-ISNULL(SUM(CASE WHEN SH.CDEPO=DEPO.KODU THEN SH.ADET ELSE 0 END),0))*ISNULL(ST.SFIYAT,0)) AS KALANTUTAR
FROM DEPO
LEFT JOIN STOKHAREKET SH ON SH.GDEPO=DEPO.KODU OR SH.CDEPO=DEPO.KODU
LEFT JOIN STOK ST ON ST.STOKKODU=SH.STOKKODU
GROUP BY YEAR(SH.TARIH),MONTH(SH.TARIH),DEPO.ADI,DEPO.KODU,SH.STOKKODU,ST.SFIYAT,ST.STOKADI
ORDER BY 2,3
