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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:31:38+00:00 2026-06-12T02:31:38+00:00

I am having some difficulty generating a query that works in the situation I

  • 0

I am having some difficulty generating a query that works in the situation I am in. Here are the details…

1 table = billing
1 table = billing dates

billing = basically when an invoice is generated, it creates a row in billing with a primary key, an invoice id, the users id, the users username, their actual name, the invoice date, the invoice total, and the payments made

billing dates = when a payment is made to an invoice, it creates a row in billing dates with a primary key, an id (which is the same as the primary key in billing table), an invoice id (same as the invoice id in billing), the users id (same as users id in billing), the date paid, the amount paid

I am trying to create an ageing report that will pull each outstanding invoice and display it in a 30/60/90/90+ table in PHP to the end user. So if invoice #12 has a $100 balance on it, along with two $10 payments and the invoice is 60 days old, it would show this info in the report. Here is what I have…

 $sql17 = "SELECT 
 $mysql_billing.login_id, $mysql_billing.primary_key, $mysql_billing.a_name,
 SUM($mysql_billing.custotal) AS finaltotal,
 SUM($mysql_billing_dates.amount) AS paidtotal,
 $mysql_billing.custotal - $mysql_billing_dates.amount AS total
 FROM $mysql_billing
 LEFT JOIN $mysql_billing_dates ON $mysql_billing.login_id = $mysql_billing_dates.login_id
 GROUP BY login_id
 ORDER BY a_name ASC";

when I run that, some are correct, while most are not. I cannot figure out what the inconsistency is. Any ideas would be greatly appreciated and maybe I am going down the wrong road?
MORE
When I do the following the correct values show…

 SELECT 
 $mysql_billing.login_id, $mysql_billing.primary_key, $mysql_billing.a_name,
 SUM($mysql_billing.custotal) AS finaltotal
 FROM $mysql_billing
 GROUP BY login_id
 ORDER BY a_name ASC

but in the original example, it doesnt always pull the correct value?

  • 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-12T02:31:40+00:00Added an answer on June 12, 2026 at 2:31 am

    Okay, I believe I understand what’s happening.

    So, presumably either table can have more than one record per login_id, which is why you’re doing the sum and group by. However, take a look at what your query gives you back when you take out the aggregates and the group by and I think you may understand the problem.

    Let’s say you have table 1 which has 2 records for login_id xxx1234 and table 2 which has 3 records for that login_id. Then when you left join them, you will get 6 records.

    e.g.

    table 1:
      login, custotal
      xxx111, 10
      xxx111, 20
    
    table 2:
      login, amount
      xxx111, 30
      xxx111, 40
      xxx111, 50
    
    after the join:
      login, custotal, amount
      xxx111 10, 30
      xxx111 10, 40
      xxx111 10, 50
      xxx111 20, 30
      xxx111 20, 40
      xxx111 20, 50
    

    So you will be potentially getting several times the amounts you expected in your sums.

    What you instead want to do is something like this:

     SELECT 
     billing_totals.login_id, billing_totals.primary_key, billing_totals.a_name, 
     billing_totals.finaltotal,
     billing_dates_totals.paidtotal,
     (billing_totals.finaltotal - billing_dates_totals.paidtotal) AS total
     FROM
       (SELECT $mysql_billing.login_id, 
       $mysql_billing.primary_key, 
       $mysql_billing.a_name, 
       SUM($mysql_billing.custotal) AS finaltotal 
       FROM $mysql_billing GROUP BY login_id) billing_totals 
     LEFT JOIN 
       (SELECT $mysql_billing_dates.login_id, 
       SUM($mysql_billing_dates.amount) AS paidtotal 
       FROM $mysql_billing_dates GROUP BY login_id) billing_dates_totals 
     ON (billing_totals.login_id = billing_dates_totals.login_id) 
     ORDER BY a_name ASC";
    

    Probably need to tweak that slightly as I don’t have a mysql to test it on at the moment.

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

Sidebar

Related Questions

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I'm having some difficulty with my Entity Framework context that is proving very troublesome
I am having some difficulty locating information of comparing C strings. I understand that
I'm having some difficulty unit testing my NHibernate repository methods, basically due to the
I am having some difficulty redirecting links. Here is a simple example and I
I'm having some difficulty presenting a ModalViewController in landscape mode. Basically it shows up
I'm having some difficulty cloning my mercurial repository over ssh. Here's what I have
I'm having some difficulty passing values and arrays between this two functions Here's the
I'm having some difficulty with NSString in my application. Basically, I have an NSString
I am having some difficulty compiling a C++ program that I've written. This program

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.