alter PROCEDURE [dbo].[spStkDINAMIKCariStokGirisCikisRaporu]  @Tarih1 DateTime, @Tarih2 DateTime  
As  
select CariKodu,CariAdi, IslemTuru, StokKodu, STOKKARTI.StokAdi,STOKKARTI.UreticiFirmaKodu,STOKKARTI.GrupAdi,   
(select ISNULL(sum(Miktar),0) from STOKHAR as SG where SG.FaturaTarihi >= @Tarih1 and SG.FaturaTarihi <= @Tarih2 and SG.CariKodu = Sh.CariKodu and SG.StokKodu = Sh.StokKodu and GC='G') as GirisMiktari,  
(select ISNULL(sum(Miktar),0) from STOKHAR as SC where SC.FaturaTarihi >= @Tarih1 and SC.FaturaTarihi <= @Tarih2 and SC.CariKodu = Sh.CariKodu and SC.StokKodu = Sh.StokKodu and GC='C') as CikisMiktari,  
(select ISNULL(sum(Miktar),0) from STOKHAR as SG where SG.FaturaTarihi >= @Tarih1 and SG.FaturaTarihi <= @Tarih2 and SG.CariKodu = Sh.CariKodu and SG.StokKodu = Sh.StokKodu and GC='G') -  
(select ISNULL(sum(Miktar),0) from STOKHAR as SC where SC.FaturaTarihi >= @Tarih1 and SC.FaturaTarihi <= @Tarih2 and SC.CariKodu = Sh.CariKodu and SC.StokKodu = Sh.StokKodu and GC='C') as Fark , 

(select ISNULL(sum(SonTutar),0) from STOKHAR as SG where SG.FaturaTarihi >= @Tarih1 and SG.FaturaTarihi <= @Tarih2 and SG.CariKodu = Sh.CariKodu and SG.StokKodu = Sh.StokKodu and GC='G') as GirisToplam,  
(select ISNULL(sum(SonTutar),0) from STOKHAR as SC where SC.FaturaTarihi >= @Tarih1 and SC.FaturaTarihi <= @Tarih2 and SC.CariKodu = Sh.CariKodu and SC.StokKodu = Sh.StokKodu and GC='C') as CikisToplam,  
(select ISNULL(sum(SonTutar),0) from STOKHAR as SG where SG.FaturaTarihi >= @Tarih1 and SG.FaturaTarihi <= @Tarih2 and SG.CariKodu = Sh.CariKodu and SG.StokKodu = Sh.StokKodu and GC='G') -  
(select ISNULL(sum(SonTutar),0) from STOKHAR as SC where SC.FaturaTarihi >= @Tarih1 and SC.FaturaTarihi <= @Tarih2 and SC.CariKodu = Sh.CariKodu and SC.StokKodu = Sh.StokKodu and GC='C') as FarkToplam  

from STOKHAR as Sh  
inner join STOKKARTI on STOKKARTI.Kodu=Sh.StokKodu  
where FaturaTarihi >= @Tarih1 and FaturaTarihi <= @Tarih2  
group by CariKodu,CariAdi, IslemTuru, StokKodu, STOKKARTI.StokAdi,STOKKARTI.UreticiFirmaKodu,STOKKARTI.GrupAdi  
order by CariKodu, IslemTuru, StokKodu  


--  [spStkDINAMIKCariStokGirisCikisRaporu] '20220101','20220301'