Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

Query1

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

Query2

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is the EDITED QUERY for all accounts

select
  a.id,
  a.account_name,
  a.op_balance,
  ifnull(e.AMOUNT1,0) as Amount1,
  ifnull(l.AMOUNT2,0) as Amount2,
  ((ifnull(SUM(e.AMOUNT1),0)-ifnull(l.AMOUNT2,0))+a.op_balance) as Balance
from accounts a
  left join (SELECT
               accounts_id,
               SUM(amount)     AS AMOUNT1
             FROM entries
               LEFT JOIN accounts
                 ON accounts.id = entries.accounts_id
             WHERE entries.side = 'D'
                 AND accounts.op_balance_dc = 'D'
             GROUP BY accounts.id) as e
    on e.accounts_id = a.id
  left join (SELECT
               accounts_id,
               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) as l
    on l.accounts_id = a.id
group by a.id   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...