|It's tricky to answer without your table definitions and some sample data, but this should get you close:
WITH cteAccountTree As
@AccountID As Account_ID
cteAccountTree As P
INNER JOIN AccountTree As C
ON C.Account_ParentID = P.Account_ID
Account_Isleaf = 0
@TotalValue = IsNull(Sum(JournalDet_Debit), 0) * CASE WHEN @Currency = 0 THEN AccountBranch_CurrencyConv ELSE 1 END
cteAccountTree As T
INNER JOIN Account_InBranch As B ON B.AccountBranch_AccountID = T.Account_ID
LEFT JOIN Journal_Details As JD ON JD.JournalDet_AccountID = T.Account_ID
LEFT JOIN Journal_Head As JH ON JH.Journal_ID = JD.JournalDet_HeadID
Journal_BranchID = @Branch
Journal_Date Between @DateFrom And @DateTo
The first part is a recursive common table expression (CTE):
How to use recursive CTE calls in T-SQL[^]
This should return the list of all accounts in the tree which have the specified account ID as an ancestor, excluding any with the
Account_Isleaf flag set.
NB: If your tree is particularly deep, you might run into the default recursion limit. There will probably be a way to work around it, but it won't be as nice as the recursive CTE solution.
You then join the tree of account IDs to your branch and journal tables to calculate the total in one hit.
The only part I'm not sure about: your code seems to be double-counting at each level:
<hr size="1" color="#63B4FF" noshade="">
<span style="color: rgba(0, 88, 170, 1)">
<cite>"These people looked deep within my soul and assigned me a number based on the order in which I joined."</cite>