No need for CREATE TABLE since there are SELECT INTO statements
Try this:SELECT UserAccount, AccountType, BillingAccountKey, BillingCode, [Rank]
FROM
(
SELECT
UserAccount = UA.UserAccounts,
UA.AccountType,
UA.BillingAccountKey,
BR.BillingCode,
BR.[Rank],
RowNum = ROW_NUMBER() OVER(PARTITION BY UA.UserAccounts ORDER BY BR.[Rank] DESC)
FROM #UserAccount UA
INNER JOIN #BillingTransactions BT
ON BT.BillingAccountKey = UA.BillingAccountKey
INNER JOIN #BillingCodeRank BR
ON BR.BillingCode = BT.BillingCode
WHERE UA.AccountType = 'O'
) SUB
WHERE SUB.RowNum = 1
ORDER BY UserAccount