SQL获取列的总数或总和

时间:2019-10-07 18:12:46

标签: oracle sqlplus

这是我当前拥有的查询。

`SELECT war.id, inv.pro_id, inv.quantity
FROM L4_Warehouses war, L4_Inventories inv, L4_Employees emp, L4_Orders ord
WHERE inv.war_id = war.id AND
      war.id = emp.war_id AND
      emp.id = ord.emp_id AND
      ord.status = 'P'
ORDER BY inv.war_id, inv.pro_id;`

此查询提供此表。

enter image description here

现在,我想要得到的最终结果是获得这些数量的总和,例如下面的示例

ID      PRO_ID      QUANTITY
1       100         18
1       101         6
1       110         6

1 个答案:

答案 0 :(得分:1)

尝试SUM和GROUP BY:

SELECT war.id, inv.pro_id, SUM(inv.quantity)
FROM L4_Warehouses war, L4_Inventories inv, L4_Employees emp, L4_Orders ord
WHERE inv.war_id = war.id AND
      war.id = emp.war_id AND
      emp.id = ord.emp_id AND
      ord.status = 'P'
GROUP BY war.id, inv.pro_id
ORDER BY inv.war_id, inv.pro_id;