mysql - SQL 2 Summations giving out incorrect results -


I am doing a query where I summarize 2 but giving it a strange result

 

What is currently happening indicates that there are 3 rows in the payments that match the selection criteria and only 1 statement, the total_ plus number is multiplied by 3 , But the pay_size is correct and then says that there are 2 items in the description that match the criteria and only 1 in payment, the pay_out number is multiplied by 2, but the total_animate is correct, any sign will be great, thanks!

Schema

  Invoice ID name .... Description ID fk_invoice_id Name Value Quantity Payment ID fk_invoice_id zodiac_pad  

More information:

I was trying to simplify my post, but just to add

customers have many invoices

I have only one In the context of the customer, therefore the customer. There is a foreign key in the invoice, I am trying to get all the invoices for a particular customer. Whatever you want to do, according to some things from DETAILS and pay per invoice. But you are joining all related records. So say that there are 3 detailed records and 2 payment records for the invoice, you can increase your payment results by multiplying 2 by 3 and increasing your payment result. You can either split:

  SELECT SUM (Details.price * Description Quantity) / Greatest (COUNT (Individual payment. ID), 1) Total amount of total amount , Deposit amount (payment. Death_pad) / COUNT (different details ID) as per the details, the amount paid on the invoice Invoice.id = details.fk_invoice_id and invoice.Id = 1 pay on invoice.Id in the job ; Payment.fk_invoice_id GROUP BY Invoice.id;  

Or just should not be involved, which should not be included. Here's a way:

  SELECT (SELECT SUM (Details; PRICE * Description Description) Where details from the details. Fk_invoice_id = Invoice.id), as the total amount, (SELECT SUM (payment Payment from payment .amount_paid) where payment is made. Fk_invoice_id = invoice.id) paid invoice as payment for WHERE id = 1;  

Here is another, where aggregation is done before joining. (We do not need an invoice table, by any means, I remove it from here.)

  SELECT d.total_amount, p.paid_amount FROM (SELECT fk_invoice_id, SUM (price * Quantity) as the total amount by the group on fk_invoice_id d) d.fk_invoice_id = invoice.iid.id (Select fk_invoice_id, SUM (payment.amount_paid) as payment for payment group from payment group by fk_invoice_id p) P.fk_invoice_id = Invoice.id WHERE d.fk_invoice_id = 1;  

As long as you are only looking for an invoice ID, all the above details can be easily added to the curriculum. For example:

  SELECT (SELECT SUM (Details.Price * Description Details) Details from where details. Fk_invoice_id = 1) Total_Account, (payment amount .fk_invoice_id = 1) Paid as_mount;  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -