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 8739521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:57:14+00:00 2026-06-13T10:57:14+00:00

My database has a structure like this: ‘Movimientos’ table keeps records of all charges

  • 0

My database has a structure like this:
enter image description here

‘Movimientos’ table keeps records of all charges and payments of my system.
‘Cargos’ table keeps some metadata of each charge on ‘Movimientos’ and ‘Abonos’ table keeps some metadata of each payment on ‘Movimientos’.

How do I write a query which sum all records in ‘Movimientos’ where ‘Abonos.tipo_abono’ and ‘Cargos.tipo_cargo’ have some criteria?

I have a query that already sum ‘Movimientos’ but I can’t add the part where ‘Abonos’ and ‘Cargos’ tables add more criteria.

SELECT SUM(M.monto) as monto, SUM(M.interes) as interes, SUM(M.iva) as iva, SUM(M.capital) as capital
                FROM movimientos AS M
                JOIN acreditados AS A ON A.id_acreditado = M.id_acreditado
                JOIN creditos AS C ON C.id_credito = A.id_credito
                WHERE  C.id_credito = 29

Let’s say I want modify the last query to just sum records where ‘Abono.tipo_abono’=1 and ‘Cargos.tipo_cargo’=1, how do I do it?

  • 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-13T10:57:15+00:00Added an answer on June 13, 2026 at 10:57 am

    If your relationships are one-to-one, you can just join the tables in and filter by your criteria. If related records are always present, make it an inner join. If the related tables may not have records, it should be a LEFT OUTER JOIN with the criteria specified in the JOIN rather than the WHERE.

    SELECT SUM(M.monto) as monto,
        SUM(M.interes) as interes,
        SUM(M.iva) as iva,
        SUM(M.capital) as capital
    FROM movimientos AS M
    JOIN acreditados AS A ON A.id_acreditado = M.id_acreditado
    JOIN creditos AS C ON C.id_credito = A.id_credito
    LEFT OUTER JOIN Abonos AS AB on AB.id_movimiento = M.id_movimiento AND AB.tipo_abono = 1
    LEFT OUTER JOIN Cargos AS CG on CG.id_movimiento = M.id_movimiento AND CG.tipo_cargo = 1
    WHERE  C.id_credito = 29
    

    However, if your relationships are many-to-one, this will affect your sum by including multiple movimientos rows. Use an EXISTS clause instead:

    SELECT SUM(M.monto) as monto,
        SUM(M.interes) as interes,
        SUM(M.iva) as iva,
        SUM(M.capital) as capital
    FROM movimientos AS M
    JOIN acreditados AS A ON A.id_acreditado = M.id_acreditado
    JOIN creditos AS C ON C.id_credito = A.id_credito
    WHERE  C.id_credito = 29
    AND EXISTS (SELECT 1
        FROM Abonos AB
        WHERE AB.id_movimiento = M.id_movimiento
        AND tipo_abono = 1
    )
    AND EXISTS (SELECT 1
        FROM Cargos CG
        WHERE CG.id_movimiento = M.id_movimiento
        AND tipo_cargo = 1
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table that has a structure like the one shown below:
The database structure is like this : each user has create their list ,
So I have a database that has a structure something like this: [user_table] user_id,
I have a MySQL database with a structure like this... Site has many Sensors
My database has a table with thousands of records. The primary key is an
I asked a question about this previously but my database structure has changed, and
I have a table in database which has following structure(data) : +--------------------------+ Organization +--------------------------+
I have a flat file source from Excel that has a structure like this:
I have a database structure like this rowid deltaValue Applicable 1 r n/d 1
I have got a table structure in mysql like this: ________________________ | id |

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.