检查。您可以通过
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
显示一些样本数据??问题在哪里? – 2014-11-01 10:56:10
这是程序的一个功能。请把整件事情判断一下。问题来自哪一行? – Ajay2707 2014-11-01 11:06:11