SQL 获取列的总计或总和
SQL Getting the total or sum of a column
这是我目前的查询。
`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;`
此查询给出此 table。
现在,我想要得到的最终结果是获得这些数量的总和,如下例
ID PRO_ID QUANTITY
1 100 18
1 101 6
1 110 6
试试 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;
这是我目前的查询。
`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;`
此查询给出此 table。
现在,我想要得到的最终结果是获得这些数量的总和,如下例
ID PRO_ID QUANTITY
1 100 18
1 101 6
1 110 6
试试 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;