/*GROUPLI*/
SELECT T.SUBEID,DR.DRID,DR.DRADI,DBO.PARAYUVARLA(SUM(OLMASIGEREKN)) OLMASIGEREKN,  DBO.PARAYUVARLA(SUM(HSTHKEDTUTAR)) HSTHKEDTUTAR,DBO.PARAYUVARLA(ABS(SUM(OLMASIGEREKN-HSTHKEDTUTAR))) FARK FROM
(
SELECT SUBEID,ITEMADI,DRID, NETHSTTUTAR, ((NETHSTTUTAR+OTOOZELTUTAR)/  ((100+HSTKDVORAN)/100)  ) KDVSIZ,HSTYZD,
HESAPISLID,HDOSYAID,
((NETHSTTUTAR+OTOOZELTUTAR)/  ((100+HSTKDVORAN)/100)  )  *HSTYZD /100   OLMASIGEREKN,HSTHKEDTUTAR,

ABS((NETHSTTUTAR+OTOOZELTUTAR/  ((100+HSTKDVORAN)/100)  )  *HSTYZD /100   -HSTHKEDTUTAR) FARK

FROM VWISLEMHAKEDIS  (NOLOCK)  
WHERE
 --HDOSYAID=4036314  AND 
 HSTYZD>0
 AND  YEAR(ITARIH)=2021 AND MONTH(ITARIH)=1
 AND ABS(((NETHSTTUTAR+OTOOZELTUTAR)/  ((100+HSTKDVORAN)/100)  )  *HSTYZD /100   -HSTHKEDTUTAR)>30
 AND HSTHKEDTUTAR>0
 ) AS T left JOIN  TDRKIMLIK DR ON DR.DRID=T.DRID
 GROUP BY T.SUBEID,DR.DRID,DR.DRADI
  HAVING SUM(OLMASIGEREKN-HSTHKEDTUTAR)<0
 ORDER BY T.SUBEID,DR.DRADI


 /*ISLEM DR GRUPLU*/
SELECT T.SUBEID,DR.DRID,DR.DRADI,HDOSYAID,ITEMADI,DBO.PARAYUVARLA(SUM(OLMASIGEREKN)) OLMASIGEREKN,  DBO.PARAYUVARLA(SUM(HSTHKEDTUTAR)) HSTHKEDTUTAR,DBO.PARAYUVARLA((SUM(OLMASIGEREKN-HSTHKEDTUTAR))) FARK FROM
(
SELECT VWISLEMHAKEDIS.SUBEID,ITEMADI,DRID, NETHSTTUTAR, ((NETHSTTUTAR+OTOOZELTUTAR)/  ((100+HSTKDVORAN)/100)  ) KDVSIZ,HSTYZD,
HESAPISLID,HDOSYAID,
((NETHSTTUTAR+OTOOZELTUTAR)/  ((100+HSTKDVORAN)/100)  )  *HSTYZD /100   OLMASIGEREKN,HSTHKEDTUTAR,

ABS((NETHSTTUTAR+OTOOZELTUTAR/  ((100+HSTKDVORAN)/100)  )  *HSTYZD /100   -HSTHKEDTUTAR) FARK

FROM VWISLEMHAKEDIS  (NOLOCK)  
WHERE
 --HDOSYAID=4036314  AND 
 HSTYZD>0
 AND  YEAR(ITARIH)=2021 --AND MONTH(ITARIH)=1
 AND ABS(((NETHSTTUTAR+OTOOZELTUTAR)/  ((100+HSTKDVORAN)/100)  )  *HSTYZD /100   -HSTHKEDTUTAR)>30
 AND HSTHKEDTUTAR>0
 ) AS T left JOIN  TDRKIMLIK DR ON DR.DRID=T.DRID
 GROUP BY T.SUBEID,DR.DRID,DR.DRADI,HDOSYAID,ITEMADI
 
 HAVING SUM(OLMASIGEREKN-HSTHKEDTUTAR)<0
 
 ORDER BY T.SUBEID,DR.DRADI,HDOSYAID,ITEMADI