with ABC as (select  StokNo,Depo,CASE WHEN GC = 'G' THEN ISNULL(SUM(Miktar), 0) WHEN GC ='C' THEN ISNULL(SUM(Miktar), 0) * -1 END Miktar,GC from STOKHAR GROUP BY GC,StokNo,Depo)
select SK.StokNo, Kodu  ,Kodu StokKodu  , SK.StokAdi , GrupAdi , UreticiFirmaKodu , OzelKod1 , OzelKod2,  SK.AnaBirim , 
	   AktifPasif,Depo, AsgariAdet,AzamiAdet,OlmasiGerekenAdet,
	   SUM(a.Miktar) Miktar,
	   (select SUM(a.Miktar) from ABC a1 where a1.StokNo = SK.StokNo and a1.Depo = a.Depo and a1.GC = 'G') Giris,
       (select SUM(a.Miktar) from ABC a1 where a1.StokNo = SK.StokNo and a1.Depo = a.Depo and a1.GC = 'C') Cikis
from  STOKKARTI as SK  
	  LEFT JOIN ABC a on a.StokNo = SK.StokNo  
WHERE SK.AktifPasif='A'      
group by SK.StokNo,Kodu ,  SK.StokAdi , GrupAdi , UreticiFirmaKodu , OzelKod1 , OzelKod2,  SK.AnaBirim , 
		 AktifPasif , a.Depo,AsgariAdet,AzamiAdet,OlmasiGerekenAdet -- having  AktifPasif='A'       


/*
(select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokNo = SK.StokNo and GC = 'G' and Depo = SH.Depo)     as [Giris],            
(select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokNo = SK.StokNo and GC = 'C' and Depo = SH.Depo)     as [Cıkıs],            
((select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokNo = SK.StokNo and GC = 'G' and Depo = SH.Depo)  -            
 (select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokNo = SK.StokNo and GC = 'C' and Depo = SH.Depo)            
)            
    as [StokAdedi],     */