There is an existing method - \Data Dictionary\Tables\DimensionHierarchy\Methods\getAccountStructure
this returns only one account structure related to a main account.
Look into this method. You can reuse the select statement inside it to fulfill your requirement.
The above method only retrieve one account structure that too if I provide recid of particular legal entity from ledger table and recid of main account from main account table .
In my case I have only list of main accounts and recid of the main accounts, and I have to find all account structures of the main accounts.
I have already explained the intention of that method.
You need to take the select in that method as reference.
May be you have to convert it into a while select and remove the condition on _ledgerRecId (in the respective where clause)