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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:37:58+00:00 2026-05-26T04:37:58+00:00

I have a query to retrieve outstanding payments from the database, which is the

  • 0

I have a query to retrieve outstanding payments from the database, which is the total invoice amount minus the total payment amount. The problem is though, when there are no payments against the invoice, these just don’t show up. I want those invoice amounts to show up, with their total payment amount being zero. Here is my query:

SELECT DISTINCT Client.FirstName + ' ' + Client.LastName                   AS
                [Client Name],
                SUM(Invoices.InvoiceAmount)                                AS
                [Total Invoice Amounts],
                SUM(ClientPayments.PaymentAmount)                          AS
                [Total Amount Paid],
                SUM(Invoices.InvoiceAmount - ClientPayments.PaymentAmount) AS
                [Outstanding Payment Amount]
FROM   Invoices
       INNER JOIN ClientPayments
         ON Invoices.InvoiceNumberID = ClientPayments.InvoiceNumberID
       INNER JOIN Client
         ON Invoices.ClientNumberID = Client.ClientNumberID
GROUP  BY Client.FirstName,
          Client.LastName  

Thanks,
Amy

  • 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-26T04:37:58+00:00Added an answer on May 26, 2026 at 4:37 am

    It probably is based on doing a left join, but would you get bogus results if you have one invoice that has been paid multiple times (partial payments). Would those “Sum” equations count the invoice amount TWICE ex: One for each partial payment? I would try as follows.

    SELECT 
          C.ClientNumberID,
          C.FirstName + ' ' + C.LastName AS ClientName, 
          SUM( I.InvoiceAmount ) TotalInvoices, 
          SUM( CP.PaymentAmount ) AS TotalAmountPaid, 
          SUM( I.InvoiceAmount ) - SUM( CP.PaymentAmount ) AS OutstandingPaymentAmount
       FROM
          Invoices I
             JOIN Clients C
                on I.ClientNumberID = C.ClientNumberID
    
          LEFT JOIN ClientPayments CP
             on I.InvoiceNumberID = CP.InvoiceNumberID
       GROUP BY
          I.ClientNumberID
       ORDER BY
          C.LastName,
          C.FirstName
    

    Notice my GROUP BY is by the client number… What would happen otherwise, if you had 5 customers “William Smith”, or “Robert Brown” which could be considered a “common name”. You don’t want to give one William Smith a statement that said he owed $5000 when he paid in full on all his purchases, while the invoices were actually split among other “William Smith” customers. THEN, the ORDER BY will put them into whatever order you need… or based on Outstanding amount descending for biggest collections at the top.

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

Sidebar

Related Questions

I have a query which retrieve from few tables but 2 tables having same
I'm trying to create a efficient query which will retrieve 'projects' from a database
I have a SQL query which retrieve wall post list $qry=mysql_query(SELECT senderid,post,date FROM .post_table.
I have a MySQL query that I use to retrieve random rows from a
I have a select query to retrieve data from tables. It is working fine,
I have to develop a layer to retrieve data from a database (can be
I have a query to retrieve some information from a table. One column in
I have the following query that retrieve the number of users per country; SELECT
I have a query where I wish to retrieve the oldest X records. At
I would like to retrieve an ordered query result, but I need to have

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.