CREATE PROCEDURE M_SelectStockIN
@FarmID int,
@ItemName varchar(50),
@InType varchar(5)
AS
/* SET NOCOUNT ON */
select
(sum((m.Item_Qty)) - sum(d.Use_Qty)) As InStock,
sum(d.Use_Qty) as total
from
MoryaSales m
inner join
DailyFarmEntry d on m.batchID = d.FarmID
where
m.batchID = @FarmID
and d.FarmID = @FarmID
and m.Tr_Type = @InType
and d.In_Type = @InType
and m.Item_Name = @ItemName
and d.ItemName = @ItemName
RETURN
我的要求是我要在d.Use_Qty
列
m.Item_Qty
答案 0 :(得分:0)
检查一下。你可以通过
添加分组CREATE PROCEDURE M_SelectStockIN
@FarmID int,
@ItemName varchar(50),
@InType varchar(5)
AS
BEGIN
/* SET NOCOUNT ON */
select (sum((m.Item_Qty)) - total) As InStock,
*
from
(
select
--you can add "batchID, tr_type" field to understand more.
--(sum((m.Item_Qty)) - sum(d.Use_Qty)) As InStock,
sum(d.Use_Qty) as total
from
MoryaSales m
inner join
DailyFarmEntry d on m.batchID = d.FarmID
where
m.batchID = @FarmID
and d.FarmID = @FarmID
and m.Tr_Type = @InType
and d.In_Type = @InType
and m.Item_Name = @ItemName
and d.ItemName = @ItemName
) a
END
已更新这是您理解的示例。
declare @item table( id int IDENTITY(1,1) NOT NULL, name varchar(50), item_qty int)
declare @DailyFarmEntry table( id int IDENTITY(1,1) NOT NULL, itemid int, Use_Qty int)
insert into @item values ('a',10),('b', 20),('c', 30),('d', 40),('e', 50)--, ('a',10)
insert into @DailyFarmEntry values (1,2),(2, 4),(3,6),(4,8),(1,3)
select * from @item i
inner join @DailyFarmEntry df on i.id = df.itemid
--create procedure
--(
declare @itemid int = 1
--)
--as
select
name,
SUM(i.item_qty) as total_qty,
SUM(df.Use_Qty) as used_total_qty,
SUM(i.item_qty) - SUM(df.Use_Qty) as remain_qty
from
@item i
inner join @DailyFarmEntry df on i.id = df.itemid
--and itemid = @itemid
group by name