Write MySQL Query

Job ID: 37856189

Budget: $25 – $50 CAD

Hi,

I have two tables in my database.

First one:

accounts (id, name, number, account_id)
journal_entry_items (id, account_id, debit, credit)

I need to get sum of each account where account sum is sum of it's own journal entry items (where account_id is account.id) and it's children. Parent is account_id. Have in mind that one account can have 10 sub elements, and those 10 elements another, and another, etc.

I also want to include only for accounts that are sub accounts of accounts where type is in ('revenue', 'expense')

It would need to be done in single, up to 2 queries, and to run efficiently on big data set.