CREATE VIEW [dbo].[TTransactionResultAccountPk]
AS
SELECT TOP 1 ATR.AccountNumber AS ResultAccountPk
, TRA.GlobalTransactionNumber, TRA.WareNumber, TRA.FolioNumber 
FROM Transactions TRA WITH (NOLOCK)
  LEFT JOIN Accounts ATR WITH (NOLOCK) ON ATR.AccountNumber = TRA.AccountNumber AND ATR.Balance = 1
--  INNER JOIN Transactions TRA1 WITH (NOLOCK) ON TRA1.GlobalTransactionNumber = TRA.GlobalTransactionNumber AND TRA1.FolioNumber = TRA.FolioNumber
ORDER BY ATR.AccountNumber DESC, TRA.TransactionNumber
