Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:12:07+00:00 2026-06-06T01:12:07+00:00

I have the following two queries. SELECT account_name,SUM(amount) AS AMOUNT1 FROM entries LEFT JOIN

  • 0

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:

  • Table Name: accounts
  • Table Name: entries

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:12:08+00:00Added an answer on June 6, 2026 at 1:12 am

    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   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
I have the following two queries: $query = $this->doctrine->em->createQuery(select u from ORM\Dynasties2\Characters u WHERE
I am currently using the following two queries: $get_forum=$this->prepare(SELECT * FROM .TBL_PREFIX.forums WHERE id=:forum_id);
I have the following two ActiveRecord queries in an application_helper.rb file: @left_menu = Page.select('id,
A quick Question. Suppose I have the following two queries: SELECT TOP 2 *
I have the following queries: Select TRUNC(create_dtime) as Day, Count(Create_Dtime) As SinS_Tot From player_chkin_hist
I have the following queries - SELECT COUNT(capture_id) as count_captures FROM captures WHERE user_id
I have the following two mysql_queries : 1. $primary_img_query = SELECT imgWidth, imgHeight FROM
I'm using parameterized queries with PHP I have the following two queries: SELECT username,question_text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.