需要在sql中从结果集中求和字段

时间:2011-09-14 21:25:12

标签: sql sql-server variables

我有项目200400,存放在6个不同的位置。

每个地点都有一个最小的库存数量,库存数量和订购数量。

这些商店中只有一个可以订购商品(mfg商店)。所以我需要将NeedToStockToStore结果添加到一个字段中

然后,我将使用该计算字段减去mfg存储订单数量,以查看商店是否过度订购。

所以我希望我的结果集与现在(下面)相同,但是还有一个用于调用OnOrderCheck的列 - 对于商店3,所有行都将为NULL,它会说1

因为mfg商店订购了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

这是我用来检索上述信息的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  

1 个答案:

答案 0 :(得分:0)

ROLLUP对于您要实现的目标非常有用。

https://docs.microsoft.com/en-us/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