我有两个表“ tbl_In_Details”和“ tbl_Out_Details” tbl_In_Details的示例数据
@code {
Component1 c1 = new Component1();
void SomeMethod()
{
c1.Tester(); // this should work
}
}
现在我要计算当前库存,即(进货-出库)为Current_Stock 我以这种方式尝试过但没有得到正确的记录,请在需要修改查询的地方引导我:
sample data for tbl_In_Details
ID Item_Name Rate Quantity Source_company
1 wire 10 4 2020-04-21 22:47:29.083
2 Tea 4 20 2020-04-21 22:47:52.823
Sample data for tbl_Out_Details
ID Item_Name Quantity Created_Date
1 wire 1 2020-04-21 22:48:48.233
2 wire 2 2020-04-21 22:50:16.367
3 Tea 2 2020-04-21 23:48:39.94
我的输出应该是
Select O.Item_Name,
sum(CAST(I.Quantity AS INT))as Incomming_Quantity ,
SUM(CAST(o.Quantity as int)) as Outgoing_Quantity ,
(sum(CAST(I.Quantity AS INT)) - SUM(CAST(o.Quantity AS INT))) As Current_Stock
from tbl_In_Details I inner join tbl_Out_Details O
ON I.Item_Name = o.Item_Name group by O.Item_Name
答案 0 :(得分:0)
使用union all
和聚合:
select item_name, sum(in_quantity) as in_quantity, sum(out_quantity) as out_quantity,
sum(in_quantity) - sum(out_quantity) as in_stock
from ((select item_name, quantity as in_quantity, 0 as out_quantity
from incoming
) union all
(select item_name, 0, quantity
from outgoing
)
) io
group by item_name;