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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:13:31+00:00 2026-05-29T07:13:31+00:00

I want to create a query to calculate the due amount, on a situation

  • 0

I want to create a query to calculate the due amount, on a situation where partial payments can be done for specific paymentSchedule id

I have a table with a payment schedule as this:

CREATE TABLE IF NOT EXISTS paymentSchedule (
  id int(11) NOT NULL AUTO_INCREMENT,
  concept varchar(32) COLLATE utf8_spanish_ci NOT NULL,
  student_id int(11) NOT NULL,
  dueDate date NOT NULL,
  amount decimal(10,2) NOT NULL,
  PRIMARY KEY (id)
)

And I have a payments transaction table like this, wherepaymentSchedule_id refers to the above table:

CREATE TABLE IF NOT EXISTS ledger (
  id int(11) NOT NULL AUTO_INCREMENT,
  paymentDate date NOT NULL,
  amount decimal(10,2) NOT NULL,
  paymentSchedule_id int(11) NOT NULL,
  PRIMARY KEY (id)
  )

I tried to to a substraction like here:
How to substract from the resut of two selects in mysql

but that only works if there are fields with the correspondinf paymentSchedule id in the ledger.
What is the right way to do it?

on the other hand, is that the right way to store payment schedules and transactions?

  • 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-05-29T07:13:32+00:00Added an answer on May 29, 2026 at 7:13 am

    It seems like you are trying to do this?

    for each id in paymentSchedule table:
        store paymentSchedule.amount - sum( ledger.amount )
    

    In which case you could do:

    SELECT paymentSchedule.id, 
           paymentSchedule.amount - IFNULL(SUM(ledger.amount),0) AS remaining
    FROM paymentSchedule
    LEFT JOIN ledger ON paymentSchedule.id=ledger.paymentSchedule_id
    GROUP BY ledger.paymentSchedule_id
    

    This shows all amounts remaining, regardless of whether a person has paid any of it off or not (thanks to the IFNULL( xxx, 0 )).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyonen help with Teradata? I want to create a query that is a
I want to create an SQL Query for a SQLite3 DB. In my table
I want to dynamically create a Drop/Create Script. For that I can calculate the
I want to create a query where I can compare all columns in a
I have a column and I want to create a query to have multiple
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
I want to create a query that selects the columns PlayerName , PlayerNumber, Location,
I want to create a query in TFS that allows me to view only
I want to create an SQL query that selects the pictures of females in
I want to create JSON object using tow query in PHP , Then retrieve

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.