[英]Need to sum field from result set in sql
I have item 200400 that is stocked at 6 different locations. 我在6个不同的位置存放有商品200400。
Each location has a minimum number they keep in stock, a quantity on hand and quantity on order. 每个地点都有其库存的最小数量,现有数量和订购数量。
Only one of these stores can order the item (mfg store). 这些商店中只有一个可以订购商品(制造商店)。 So I need to add the NeedToStockToStore results into one field
所以我需要将NeedToStockToStore结果添加到一个字段中
I will then use that calculated field to subtract the mfg stores order quantity to see if the store is over ordering. 然后,我将使用该计算出的字段减去mfg商店的订单数量,以查看商店是否超额订购。
So I want my result set to be the same as it is now(below) but have one more column for called OnOrderCheck – all rows will be NULL expect for store 3 where it would say 1 因此,我希望我的结果集与现在(下)相同,但又有一个用于OnOrderCheck的列–所有行都将为NULL,而对于存储3来说,它会说1
Because mfg store ordered 6 but they only needed to order 5 so they have ordered 1 to many. 因为mfg store订购了6个,但是他们只需要订购5个,所以他们订购了1对许多。
item desc store store_min store_max on_hand on_order mfg store NeedToStockToStore
200400 SVAssembly 1 1 1 0 0 3 1
200400 SVAssembly 2 1 1 0 0 3 1
200400 SVAssembly 3 1 1 0 6 3 1
200400 SVAssembly 5 1 1 0 0 3 1
200400 SVAssembly 7 1 1 1 0 3 0
200400 SVAssembly 8 1 1 0 0 3 1
This is the SQL Code I am using to retrieve the above information: 这是我用来检索以上信息的SQL代码:
SELECT T0.[ItemCode],
T0.[ItemName],
T2.[WhsCode] as [Whse],
T2.[MinStock] as [Store Min],
T2.[MaxStock] as [Store Max],
T2.[OnHand],
T2.[OnOrder] as [On PO],
T0.[MinLevel] as [Corp Min],
T0.[MaxLevel] as [Corp Max],
T0.U_MFGSite,
MAX(CASE
WHEN T2.[WhsCode] = T0.[U_MFGSite] THEN T2.[MinStock] - T2.[OnHand]
ELSE ( CASE
WHEN T2.[OnHand] < T2.[MinStock] THEN
( T2.[MinStock] - (
T2.[OnHand] + T2.[OnOrder] ) )
ELSE 0
END )
END) as NeedToStockToStore
FROM asap.dbo.OITM T0
INNER JOIN asap.dbo.OITW T2
ON T0.ItemCode = T2.ItemCode
WHERE t0.itemcode = '200400'
and t2.minstock <> 0
and t2.maxstock <> 0
group by T0.[ItemCode],
T0.[ItemName],
T2.[WhsCode],
T2.[MinStock],
T2.[MaxStock],
T2.[OnHand],
T2.[OnOrder],
T0.[MinLevel],
T0.[MaxLevel],
t0.U_mfgsite
WITH ROLLUP would work great for what you are trying to achieve. WITH ROLLUP对于您要实现的目标非常有用。
https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-2017#group-by-rollup https://docs.microsoft.com/zh-cn/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-2017#group-by-rollup
SELECT T0.[ItemCode],
T0.[ItemName],
T2.[WhsCode] as [Whse],
T2.[MinStock] as [Store Min],
T2.[MaxStock] as [Store Max],
T2.[OnHand],
T2.[OnOrder] as [On PO],
T0.[MinLevel] as [Corp Min],
T0.[MaxLevel] as [Corp Max],
T0.U_MFGSite,
MAX(CASE
WHEN T2.[WhsCode] = T0.[U_MFGSite] THEN T2.[MinStock] - T2.[OnHand]
ELSE ( CASE
WHEN T2.[OnHand] < T2.[MinStock] THEN
( T2.[MinStock] - (
T2.[OnHand] + T2.[OnOrder] ) )
ELSE 0
END )
END) as NeedToStockToStore
FROM asap.dbo.OITM T0
INNER JOIN asap.dbo.OITW T2
ON T0.ItemCode = T2.ItemCode
WHERE t0.itemcode = '200400'
and t2.minstock <> 0
and t2.maxstock <> 0
group by T0.[ItemCode],
T0.[ItemName],
T2.[WhsCode],
T2.[MinStock],
T2.[MaxStock],
T2.[OnHand],
T2.[OnOrder],
T0.[MinLevel],
T0.[MaxLevel],
t0.U_mfgsite WITH ROLLUP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.