Hi All,
I am very bad at formatting,please dont mine.Let me know if it works for you!!
SELECT
QL.USERAccounts
, QL.Accounttype
, QL.Billingcode
, QL.Rank
--, Ql.seq
FROM
(SELECT
USERAccounts
, Accounttype
, UA.BillingAccountKey
, BCR.Rank
, BCR.BillingCode
, ROW_NUMBER() OVER(PARTITION BY UserAccounts ORDER BY UserAccounts) as seq
FROM #UserAccount UA
INNER JOIN #BillingTransactions BT ON UA.BillingAccountKey=BT.BillingAccountKey
INNER JOIN #BillingCodeRank BCR ON BT.BillingCode=BCR.BillingCode
WHERE AccountType='O'
)AS QL
WHERE
seq=1
ORDER BY
QL.UserAccounts