【发布时间】:2020-11-12 18:41:30
【问题描述】:
我有两个表,我对它们执行了内部连接: 以下是我的查询
SELECT a.ship_id AS ship_id,
sum(a.qty) as total_qty
from table_a as a
inner join table_b on a.shp_id = b.shpid
group by a.shp_id
Sample data:
table_a
-----------------------------
product_name | qty | ship_id
-----------------------------
item_1 | 10 | 1
item_2 | 20 | 1
item_3 | 10 | 2
item_4 | 10 | 2
table_b
-------------------
ship_id | desc
-------------------
1 | desc_1
2 | desc_2
我得到上述查询的输出如下:
--------------------
ship_id | total_qty
--------------------
1 | 60
2 | 40
预期输出:
--------------------
ship_id | total_qty
--------------------
1 | 30
2 | 20
谁能帮我弄到这个。
【问题讨论】:
-
您的查询看起来不错,根据您提供的数据应该返回您想要的结果。
标签: sql sum inner-join