SELECT YEAR(dbo.stokhareket.TARIH) AS YIL,MONTH(dbo.stokhareket.TARIH) AS AY,dbo.DEPO.ADI AS DEPOADI, dbo.DEPO.KODU AS DEPOKODU, ST.GRUP,dbo.stokhareket.STOKKODU, ST.STOKADI,
ISNULL((SELECT CONVERT(DECIMAL(10,3),SUM(ADET)) FROM  STOKHAREKET WHERE GDEPO = DEPO.KODU AND STOKKODU = DBO.STOKhareket.STOKKODU), 0)  - ISNULL((SELECT CONVERT(DECIMAL(10,3),SUM(ADET))FROM STOKHAREKET
     WHERE CDEPO = DEPO.KODU AND STOKKODU = DBO.STOKhareket.STOKKODU), 0) as StokAdedi
,
ST.SFIYAT as SonAlisFiyat,
CONVERT(DECIMAL(10,2),(ISNULL((SELECT CONVERT(DECIMAL(10,3),SUM(ADET)) FROM  STOKHAREKET WHERE GDEPO = DEPO.KODU AND STOKKODU = DBO.STOKhareket.STOKKODU), 0)  - ISNULL((SELECT CONVERT(DECIMAL(10,3),SUM(ADET))FROM STOKHAREKET
     WHERE CDEPO = DEPO.KODU AND STOKKODU = DBO.STOKhareket.STOKKODU), 0))*CONVERT(DECIMAL(10,2),ST.SFIYAT,2)) as SonFiyatTutar,
ISNULL((SELECT CONVERT(DECIMAL(10,3),AVG(FIYAT)) FROM STOKHAREKET S WHERE S.GDEPO = DEPO.KODU AND S.STOKKODU = STOKHAREKET.STOKKODU), 0) AS OrtalamaFiyat,
CONVERT(DECIMAL(10,2),(ISNULL((SELECT CONVERT(DECIMAL(10,3),SUM(ADET)) FROM  STOKHAREKET WHERE GDEPO = DEPO.KODU AND STOKKODU = DBO.STOKhareket.STOKKODU), 0)  - ISNULL((SELECT CONVERT(DECIMAL(10,3),SUM(ADET))FROM STOKHAREKET
     WHERE CDEPO = DEPO.KODU AND STOKKODU = DBO.STOKhareket.STOKKODU), 0))*ISNULL((SELECT CONVERT(DECIMAL(10,3),AVG(FIYAT)) FROM STOKHAREKET S WHERE S.GDEPO = DEPO.KODU AND S.STOKKODU = STOKHAREKET.STOKKODU), 0)) as OrtalamaFiyatTutar
FROM  dbo.STOK ST RIGHT OUTER JOIN
                      dbo.stokhareket ON ST.STOKKODU = dbo.stokhareket.STOKKODU RIGHT OUTER JOIN
                      dbo.DEPO ON dbo.DEPO.KODU = dbo.stokhareket.GDEPO OR dbo.DEPO.KODU = dbo.stokhareket.CDEPO
GROUP BY YEAR(dbo.stokhareket.TARIH),MONTH(dbo.stokhareket.TARIH),dateadd(year, datediff(year, 0, dbo.stokhareket.TARIH), 0),dateadd(MONTH, datediff(MONTH, 0, dbo.stokhareket.TARIH), 0),dbo.DEPO.ADI, dbo.DEPO.KODU, ST.GRUP,dbo.stokhareket.STOKKODU, ST.STOKADI,ST.SFIYAT
ORDER BY ST.GRUP,dbo.stokhareket.STOKKODU ASC