Select Sum from two joined tables

2019-07-17 03:04发布

问题:

There are structures:

CREATE TABLE `invoices` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `invoices` VALUES (1,'2018-09-22');

CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`invoice_id` int(10) unsigned NOT NULL,
`amount` decimal(10,2) unsigned NOT NULL,
`quantity` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `products` VALUES (1,1,150.00,2),(2,1,60.00,3),(3,1,50.00,1);

CREATE TABLE `payments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`invoice_id` int(10) unsigned NOT NULL,
`amount` decimal(10,2) unsigned NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `payments` VALUES (1,1,400.00,'2018-09-23'),(2,1,80.00,'2018-09-23');

I have this query:

select i.id, sum(pr.amount * pr.quantity) as productAmount, 
sum(pm.amount) as paymentAmount
from invoices as i
left join products as pr on pr.invoice_id=i.id
left join payments as pm on pm.invoice_id=i.id
group by i.id

and have this result:

+----+---------------+---------------+
| id | productAmount | paymentAmount |
+----+---------------+---------------+
|  1 |       1060.00 |       1440.00 |
+----+---------------+---------------+
1 row in set (0,00 sec)

However, I want to get the following result:

+----+---------------+---------------+
| id | productAmount | paymentAmount |
+----+---------------+---------------+
|  1 |        530.00 |        480.00 |
+----+---------------+---------------+
1 row in set (0,00 sec)

I want sum amount of products and sum amount of payments grouped by invoice.id.
What should be the query in this case?

回答1:

I do face this kind of queries at times. Due to multiple joins, values from a particular table get duplicated, triplicated etc. To fix this, I normally do a small hack by dividing the sum (on a particular table) by the count of distinct Id(s) from the other table. This negates the effect of multiple duplicates happening.

Try the following query:

select i.id, 
       (sum(pr.amount * pr.quantity) / IF(count(distinct pm.id) > 0, count(distinct pm.id), 1) as productAmount, 
       (sum(pm.amount) / IF(count(distinct pr.id) > 0, count(distinct pr.id), 1) as paymentAmount
from invoices as i
left join products as pr on pr.invoice_id=i.id
left join payments as pm on pm.invoice_id=i.id
group by i.id


回答2:

Use the below sub query to get your expect result

SELECT id, 
(select sum(pr.amount * pr.quantity) from products as pr where pr.invoice_id=i.id ) as productAmt, 
(select sum(amount) from payments where invoice_id=i.id ) as PaymentAmt 
FROM `invoices` i order by id asc


回答3:

Try this: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=3b496214bf0ffb517dcaa9be7f0b6bb7

select a.id,pramount,sum(amount) as paymentamount from 
(select i.id,sum(pr.amount *pr.quantity) as pramount
from invoices as i
join products as pr on pr.invoice_id=i.id
group by i.id)a
join payments pm on a.id=pm.invoice_id
group by a.id


回答4:

I can imagine a situation where an invoice is created a products is created but no payment, similarly an invoice is created a payment is created but no products .So you could create sub queries for the products and payments

drop table if exists i,pr,pm;
CREATE TABLE `i` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `i` VALUES (1,'2018-09-22'),(2,'2018-09-22'),(3,'2018-09-22'),(4,'2018-09-22');

CREATE TABLE `pr` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`invoice_id` int(10) unsigned NOT NULL,
`amount` decimal(10,2) unsigned NOT NULL,
`quantity` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `pr` VALUES (1,1,150.00,2),(2,1,60.00,3),(3,1,50.00,1),(4,3,50.00,1);

CREATE TABLE `pm` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`invoice_id` int(10) unsigned NOT NULL,
`amount` decimal(10,2) unsigned NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `pm` VALUES (1,1,400.00,'2018-09-23'),(2,1,80.00,'2018-09-23'),(3,4,80.00,'2018-09-23');


select i.id, (select sum(pr.amount * pr.quantity) from pr where pr.invoice_id = i.id) as productAmount, 
                 (select sum(pm.amount) from pm where pm.invoice_id = i.id) as paymentAmount
from i
having productAmount > 0 or paymentAmount > 0;

You may (or may not want the having clause)

+----+---------------+---------------+
| id | productAmount | paymentAmount |
+----+---------------+---------------+
|  1 |        530.00 |        480.00 |
|  3 |         50.00 |          NULL |
|  4 |          NULL |         80.00 |
+----+---------------+---------------+
3 rows in set (0.00 sec)