SQL SUM函数获取库存余额
SQL SUM function to get inventory balance
更新:
我有 2 个销售产品的地点,每个地点有 1 个 table ...我在下图中显示的每个地点都有余额 ...现在我想将一个余额中的余额总和,我想获得物品的全部余额(2 个地点的 SUM 余额)......需要你的帮助
我使用此查询来获取上面显示的余额
Select item_id , sum(purchase)-sum(sales)as total from location_1 group by item_id
HAVING sum(purchase)-sum(sales) <> 0
UNION ALL
Select item_id, sum(purchase)-sum(sales) as total from location_2 group by item_id,
HAVING sum(purchase)-sum(sales) <> 0
您可以尝试去掉表格中 itemid 列末尾的数字,然后合并它们。
SELECT t.itemid, sum(purchase)- sum(sales) as balance
FROM (
SELECT left(itemid, length(itemid) -1) as itemid,
sales,
purchase
FROM Location_1
UNION ALL
SELECT left(itemid, length(itemid) -1) as itemid,
sales,
purchase
FROM Location_2
) AS t
GROUP BY t.itemid;
更新:
我有 2 个销售产品的地点,每个地点有 1 个 table ...我在下图中显示的每个地点都有余额 ...现在我想将一个余额中的余额总和,我想获得物品的全部余额(2 个地点的 SUM 余额)......需要你的帮助
我使用此查询来获取上面显示的余额
Select item_id , sum(purchase)-sum(sales)as total from location_1 group by item_id
HAVING sum(purchase)-sum(sales) <> 0
UNION ALL
Select item_id, sum(purchase)-sum(sales) as total from location_2 group by item_id,
HAVING sum(purchase)-sum(sales) <> 0
您可以尝试去掉表格中 itemid 列末尾的数字,然后合并它们。
SELECT t.itemid, sum(purchase)- sum(sales) as balance
FROM (
SELECT left(itemid, length(itemid) -1) as itemid,
sales,
purchase
FROM Location_1
UNION ALL
SELECT left(itemid, length(itemid) -1) as itemid,
sales,
purchase
FROM Location_2
) AS t
GROUP BY t.itemid;