MySQL specific column addition -


i have 3 tables:

bank_account(tran_id, pay_id, expense_id, amount, balance)  payment(pay_id, client_id, date_payment, amount)  expense(expense_id, emp_id, amount, expense)  

in tran_id primary key bank_account , same pay_id payment , expense_id expense well. in bank_account when pay_id there expense_id null , vice versa. want calculate profit of company using bank_account. how can use sql script calculate (the sum of pay_id based amount - sum of expense_id based amount).

i think, should statement, think, miss field group in bank_account table (i.e. company). wants know, amount company, or?

select <missingfieldcompany>, sum(payment.amount) payments, sum(expense.amount) expenses, sum(payment.amount) - sum(expense.amount) overall bank_account  left join payment on payment.pay_id = bank_account.pay_id left join expense on expense.expense_id = bank_account.expense_id group <missingfieldcompany> 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -