如何从不同的表中计算数量? [英] How to calculate quantity from different table ?
本文介绍了如何从不同的表中计算数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个表:tbl1,tbl2,tbl3。每个表都有quantity和ItemID列。我需要在这些表中按ItemId搜索并获得商店程序中的总数量。
实际上我需要(tbl1 + tbl2- tbl3)=具有ItemId的totalqty
Plz帮助
推荐答案
不确定您的问题,希望这可以帮助您。
如果您的列数据类型相似,您可以在sql中使用'union all'。
如果您需要Quantity列表,请尝试这个,
Not sure with your question,Hope this may help you.
If your column datatype are similar you can use 'union all' in sql.
If you need list of Quantity column,then try like this,
declare @ItemId int=1;
with cte as (select Quantity,ItemId from tbl1 a union all
select Quantity,ItemId from tbl2 b union all
select Quantity,ItemId from tbl3 c) select Quantity from cte where ItemId=@ItemId
//它将返回数量列列表
如果您需要数量总和,请执行此操作,
//It will return list of quantity column
If you need Sum of quantity,Do like this,
declare @ItemId int=1;
with cte as (select Quantity,ItemId from tbl1 a union all
select Quantity,ItemId from tbl2 b union all
select Quantity,ItemId from tbl3 c) select SUM(Quantity) as TotalQuantity from cte where ItemId=@ItemId
这篇关于如何从不同的表中计算数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文