I have the following two queries.
SELECT
account_name,SUM(amount) AS AMOUNT1
FROM
entries
LEFT JOIN
accounts ON accounts.id = entries.accounts_id
WHERE
side = 'D' AND op_balance_dc = 'D'
GROUP BY
accounts.id
Here's the result of the query:
SELECT
account_name,SUM(amount) AS AMOUNT2
FROM
entries
LEFT JOIN
accounts ON accounts.id = entries.accounts_id
WHERE
side = 'C' AND op_balance_dc = 'D'
GROUP BY
accounts.id
Here's the result of the second query
I am not trying to display the results of the above two queries, what I am trying to achieve is get the account_name, AMOUNT1 and AMOUNT2 FROM the above queries and then subtract Amount2 FROM AMOUNT1- and then add a value from my table's column.
Example:
(AMOUNT1-AMOUNT2)+op_balance //here op_balance is my column name
and then display the account_name
and (AMOUNT1-AMOUNT2)+op_balance
Could you please help me with this query?
Thanks :)
Please let me know if you need anymore information.:)
Edited
Here's the structure of my tables:
Thanks :)
Edit2
Table Name: entries (Case-1)
id trans_id accounts_id amount side
1 1 12 1000 D
2 1 1 1000 C
7 4 1 14000 D
8 4 2 14000 C
Table Name: entries (Case-2)
id trans_id accounts_id amount side
1 1 12 1000 D
2 1 1 1000 C
Your code will work for the case-1 but not for case-2. You used lentries.accounts_id = entries.accounts_id
in your code and that is why since there's no multiple values for the same accounts_id
in the table to join and your code is not counting the values in case-2.
But I am trying to get all the data. Thanks :)
See Question&Answers more detail:os