Вообще-то, это банальный вопрос приоритета. По принципу, "кто первый встал, того и тапки". Соответственно, решается с помощью Order by в подзапросе
X++:
select
(select top 1 SumAccount
from CustLedgerAccounts
where CustLedgerAccounts.PostingProfile = custTrans.postingProfile
and CustLedgerAccounts.num = (case CustLedgerAccounts.AccountCode
when 0 then custtable.AccountNum
when 1 then custTable.CustGroup
else CustLedgerAccounts.num
end)
and CustLedgerAccounts.dataAreaId = 'dat'
order by AccountCode ---- <----
) as pSumAccount
,custTrans.*
from custtrans
inner join custtable on custTable.AccountNum = custTrans.AccountNum
where custTrans.DataAreaId = 'dat'
and custTable.dataAreaId = 'dat'