select distinct B.adDate, B.acKey, B.acKeyView, B.anClerk, B.acCurrency, 
    V.acKey POTNALKLJUC, B.acDocType, B.acSubject, B.acDept, B.acPayMethod,
    B.acType, B.anPaid, B.acNote, 
    (select sum(BP.anAmount) sumarno
                   from tHE_CashReg BQ
                   join tHE_CashRegItem BP on BQ.acKey = BP.acKey
                   join tHR_LinkTrvlCashRegItem VQ on BP.anNo = VQ.anLnkNo and BP.acKey = VQ.acLnkKey
                   where VQ.acType = '<' and VQ.acKey = V.acKey
					and BQ.acCurrency = B.acCurrency
					and BQ.acKey = B.acKey) CalcZNESEK
from vHE_CashReg B
join tHE_CashRegItem BP on BP.acKey = B.acKey
join tHR_LinkTrvlCashRegItem V on BP.anNo = V.anLnkNo and B.acKey = V.acLnkKey 
where V.acType = '<' and V.acKey = @acKey
order by 1

