【发布时间】:2021-02-03 08:07:36
【问题描述】:
我有两张桌子:
表 A
发票日期 |客户 ID |装运编号 |重量 |产品价格
表 B
发票日期 |客户 ID |装运编号 |重量
我正在尝试创建一个新表 C,它使用 sum(product_price) 计算一个新列: 所以我期待类似的东西: (每个客户可以有许多具有相同shipping_id但product_price发生变化的货物 对于每个不同的 shipping_id ) (表 B 中缺少一些权重)
示例: 表 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
表 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
新表 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
我的代码:
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
sum(product_price) 计算错误,我不明白为什么..
【问题讨论】:
-
请解释
distinct(weight)。另外,标记您正在使用的数据库。 -
任何表中都没有列id。您是否尝试过在没有 SUM 的情况下运行查询来检查行中的产品价格?您是否需要执行 JOIN,因为您只从 A 表中选择列?
标签: sql join sum right-join