I have two tables:
table A
invoicedate | client_id | shipment_id | weight | product_price
table B
invoicedate | client_id | shipment_id | weight
I am trying to create a new table C, which calculates a new column with the sum(product_price): So I am expecting something like: (Each client can have many shipments with the same shipment_id but the product_price changes for each distinct shipment_id ) (In table B some weights are missing)
Example: Table A
invoicedate | client_id | shipment_id | weight | product_price
12/10/19 1111 888 48 36
13/11/19 2222 111 30 45
12/10/19 1111 888 48 125
12/10/19 1111 888 48 127.2
Table B
invoicedate | client_id | shipment_id | weight
12/10/19 1111 888 48
13/11/19 2222 111 30
12/10/19 1111 888 -
12/10/19 1111 888 48
New Table C
distinct(client_id)|invoicedate | distinct(weight) | total_sum(product_price)
1111 | 12/10/19 | 1111 | (36+125+127.2)
2222 | 13/11/19 | 2222 | 45
My code:
create table C as
select A.invoicedate,A.shipment_id,A.weight,sum(A.product_price) as sum_product_price
from A
right join B on B.id=A.id
group by A.id, A.weight
The sum(product_price) is calculated wrong and I dont understand why..