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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:07:29+00:00 2026-06-11T06:07:29+00:00

I am trying to create a query that will take information out of four

  • 0

I am trying to create a query that will take information out of four tables for a billing system that I am creating. I have the following tables:

Table Invoice

InvoiceID (PK)
ClientID
Date
Status
...

Table Client

ClientID (PK)
ClientName
...

Table InvoiceItem

ItemID (PK)
InvoiceID
Amount
...

Table Payments

PaymentID (PK)
InvoiceID
Amount
...

I need to create a query where I can access information from the Invoice table along with the client name, and the sum of all invoice items and payments associated with the invoice.

I have tried the following:

SELECT 
    Invoice.InvoiceID, 
    Invoice.`Date`, 
    Invoice.Terms, 
    Invoice.DateDue, 
    Invoice.Status, 
    Client.ClinicName, 
    SUM(InvoiceItem.Amount), 
    SUM(Payment.PaymentAmount)
FROM Invoice
JOIN (Client, InvoiceItem, Payment) ON
    (Client.ClientID=Invoice.ClientID AND
     InvoiceItem.InvoiceID=Invoice.InvoiceID AND 
     Payment.InvoiceID=Invoice.InvoiceID)

And while this kind-of works, it is multiplying the SUM() by the number of records used to get the sum (i.e. if there are two payments – 800,400 – It gives me (800+400)*2 — 2400). I am guessing that there is something with how I am using the join, and I have honestly never had to use join for more than one table, and I would always use GROUP BY, but I can’t seem to get that to work correctly.

To make matters worse, I have been lost to the world of vb.net/MSSQL client-side programming for the past several years, so my MySQL is rather rough.

  • 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-11T06:07:31+00:00Added an answer on June 11, 2026 at 6:07 am

    Your problem is that you can’t aggregate over two independent tables at once in a single query. However you can do it using subqueries.

    SELECT Invoice.InvoiceID, Invoice.`Date`, Invoice.Terms, Invoice.DateDue, Invoice.Status, Client.ClinicName, InvoiceItemSum.SumOfAmount, PaymentSum.SumOfPaymentAmount
      FROM Invoice
      INNER JOIN Client ON Client.ClientID = Invoice.ClientID
      INNER JOIN (
        SELECT InvoiceID, SUM(Amount) AS SumOfAmount
          FROM InvoiceItem
          GROUP BY InvoiceID
      ) InvoiceItemSum ON InvoiceItemSum.InvoiceID = Invoice.InvoiceID
      INNER JOIN (
        SELECT InvoiceID, SUM(PaymentAmount) AS SumOfPaymentAmount
        FROM Payment
        GROUP BY InvoiceID
      ) PaymentSum ON PaymentSum.InvoiceID = Invoice.InvoiceID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a single query that will combine the following two queries.
Trying to create a mysql query that will do the following: For each record
I'm trying to create a created_at query range that will capture all records created
We are trying to create/query information from a CF based on the following structure
I'm trying to create a query, that will calculate sum of products on invoice.
I'm trying to create a query that will pull the first 10 records of
I am trying to create an HQL query that will filter a tree based
I am trying to create a query that will show the details for each
I am trying to create a single query that will pull results from a
I am trying to create a query in MS SQL 2005 that will return

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.