【问题标题】:Find Running Total In sql - if 1st column has no value then calculate from 2nd column在 sql 中查找运行总计 - 如果第一列没有值,则从第二列计算
【发布时间】:2022-01-23 08:25:04
【问题描述】:
declare @tbl as table
(
    ItemId int,
    SOQty int,
    DIQty int ,
    IssueQty int,
    BalanceQty int,
    CreateDate datetime,
    StockQty int,
    WIPQty int
)

insert into @tbl values 
(1,10,10,0,10,'2021-12-16 19:28:32.200',10,0), 
--(2,5,5,1,4,'2021-12-17 19:28:05.200',80),
(1,15,10,10,5,'2021-12-18 19:28:34.200',30, 0),
(1,8,5,2,2,'2021-12-19 19:28:35.200',30,0)
--(2,15,15,0,15,'2021-12-20 19:28:05.200',80),
--(2,12,10,5,5,'2021-12-22 19:28:05.200',80)
--(1,15,10,10,5,'2021-12-18 19:28:34.200',30,0)
 
  
update x 
set x.StockQty = tx.StockQty  
from @tbl x
join 
    (select * 
     from 
         (select 
              *,
              row_number() over (partition by itemid order by CreateDate) as RowNo 
          from @tbl) as t 
     where t.RowNo = 1) as tx on tx.CreateDate = x.CreateDate
 
update x 
set x.StockQty = 0 
from @tbl x
join 
    (select * 
     from 
         (select 
              *,
              row_number() over (partition by itemid order by CreateDate) as RowNo 
          from @tbl) as t 
     where t.RowNo != 1) as tx on tx.CreateDate = x.CreateDate
 

declare @tbl1 as table
(
    ItemId int,
    SOQty int,
    DIQty int ,
    IssueQty int,
    BalanceQty int,
    CreateDate datetime,
    StockQty int,
    WIPQty int,
    StockAllocateQty int,
    UpdatedStockQty int
)
 
insert into @tbl1
    select 
        *, 
        BalanceQty as StockAllocateQty,
        sum(StockQty - BalanceQty) over (partition by ItemId 
                                         order by CreateDate   
                                         rows between unbounded preceding and current row) as UpdatedStockQty  
    from @tbl 
    -- order by CreateDate
 
 declare @tblItemWIPQty table
 (
 ItemId int,
 WIPQty  int
 )

 insert into @tblItemWIPQty values(1,40)
 
 
 
update x set x.WIPQty =  tt.WIPQty from @tbl1 x
join 
(select * from  
(
select top 1 * from @tbl1 where UpdatedStockQty < 0
) as t) as t on t.CreateDate = x.CreateDate 
join @tblItemWIPQty tt on tt.ItemId = x.ItemId
 

 
select *,BalanceQty as AllocateQtyWIP ,SUM(case when StockQty - BalanceQty >= 0 then StockQty -BalanceQty else WIPQty - BalanceQty end) 
over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as UpdatedStockQtyWIP  from @tbl1 
--ORDER BY CreateDate

 

我想先从 StockQty 分配 BalanceQty,当 StockQty 完成后从 WIPQty 分配

这工作正常(以下只是为了理解)。

对于第一行 BalanceQty = 10 ,StockQty=10 所以如果我们从 Balance 中分配所有 10 个 Qty,即 StockAllocateQty = 10 和 UpdatedStockQty = 0。(Balance - StockQty)= UpdatedStockQty

对于第二行 BalanceQty = 5,StockQty = 0(所有库存数量都在第一行使用)所以如果我们从余额中分配 5 个数量,那么我们将得到 StockAllocateQty = 5 和 UpdatedStockQty = -5。 (0 - 5) = -5

对于第 3 行 BalanceQty = 2,StockQty = 0(所有库存 Qty 在第 1 行使用)所以如果我们从余额中分配 2 个 Qty,那么我们将得到 StockAllocateQty = -7 和 UpdatedStockQty = -5。 (-5 - -2) = -7

问题出在这 2 列 AllocateQtyWIP UpdatedStockQtyWIP

现在我有额外的工作在进行中,如果所有库存数量都用在第一行,那么我有额外的工作要分配,所以我分配到第二行

对于我们从库存数量使用的第一行,我们甚至没有使用 WIP 数量而不是它应该是 AllocateQtyWIP = 0 UpdatedStockQtyWIP = 40 但我得到 AllocateQtyWIP = 10, UpdatedStockQtyWIP = 0

这两列的预期输出:

AllocateQtyWIP UpdatedStockQtyWIP
0 40
5 35
2 33

但我却得到了这个:

ItemId SOQty DIQty IssueQty BalanceQty CreateDate StockQty WIPQty StockAllocateQty UpdatedStockQty AllocateQtyWIP UpdatedStockQtyWIP
1 10 10 0 10 2021-12-16 19:28:32.200 10 0 10 0 10 0
1 15 10 10 5 2021-12-18 19:28:34.200 0 40 5 -5 5 35
1 8 5 2 2 2021-12-19 19:28:35.200 0 0 2 -7 2 33

预料之中

ItemId SOQty DIQty IssueQty BalanceQty CreateDate StockQty WIPQty StockAllocateQty UpdatedStockQty AllocateQtyWIP UpdatedStockQtyWIP
1 10 10 0 10 2021-12-16 19:28:32.200 10 0 10 0 0 40
1 15 10 10 5 2021-12-18 19:28:34.200 0 40 5 -5 5 35
1 8 5 2 2 2021-12-19 19:28:35.200 0 0 2 -7 2 33

【问题讨论】:

  • 不要对我的问题投反对票,请提供理由,我会改进我的问题
  • 只有我一个人吗? ...获取/预期的输出示例似乎具有相同的值。
  • 如果您仔细查看 AllocateQtyWIP ,第一行的UpdatedStockQtyWIP 列会有所不同
  • 好的,是的,对于 that 表,我说的是最后两个输出示例?
  • 是的,最后 2 列有一个运行总计,我试过但没有得到预期的输出

标签: sql sql-server-2008 sql-server-2005 sql-server-2012


【解决方案1】:
 declare @tbl as table
(
 ItemId int,  
 BalanceQty int,
 CreateDate datetime,
 StockQty int,
 WIPQty int
)


insert into @tbl values 
(1,10,'2021-12-16 19:28:32.200',30,0), 
(1,5,'2021-12-18 19:28:34.200',30,0),
(1,2,'2021-12-19 19:28:35.200',30,0)
  
update x set x.StockQty = tx.StockQty  from @tbl x
join 
(select * from 
(
select *,ROW_NUMBER()over(partition by itemid order by CreateDate) as RowNo from @tbl  
)as t where t.RowNo = 1) as tx on tx.CreateDate = x.CreateDate
 

 update x set x.StockQty = 0 from @tbl x
join 
(select * from 
(
select *,ROW_NUMBER()over(partition by itemid order by CreateDate) as RowNo from @tbl  
)as t where t.RowNo != 1) as tx on tx.CreateDate = x.CreateDate
 

 declare @tbl1 as table
(
 ItemId int, 
 BalanceQty int,
 CreateDate datetime,
 StockQty int,
 WIPQty int,
 allocateQTy int,
 UpdatedStockQty int,
 WIPQty1 int
)
 
 insert into @tbl1
select *,BalanceQty as allocateQTy ,SUM(StockQty - BalanceQty) 
over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as UpdatedStockQty,40  from @tbl 
 
 
 declare @tblWIPQty table
 (
 ItemId int,
 StockQty int
 )

insert into @tblWIPQty values(1,40)
 
declare @tbl2 as table
(
ItemId int, 
BalanceQty int,
CreateDate datetime,
StockQty int,
WIPQty int,
allocateQTy int,
UpdatedStockQty int,
WIPQty1 int,
allocateQTyWIP int,
UpdatedStockQtyWIP int
 
)
  

 update x set x.WIPQty =  tt.StockQty  from @tbl1 x
join 
(select * from  
(
select top 1 * from @tbl1 where UpdatedStockQty < 0
) as t) as t on t.CreateDate = x.CreateDate 
join @tblWIPQty tt on tt.ItemId = x.ItemId

 
 
insert into @tbl2 
select *,
case when SUM(StockQty - BalanceQty) over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) >= 0 then 0 else BalanceQty end  as AllocateQtyWIP ,
case when SUM(StockQty - BalanceQty) over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) < 0 then SUM(case when StockQty - BalanceQty >= 0 then StockQty - BalanceQty else WIPQty - BalanceQty end) over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) else WIPQty1 end as UpdatedStockQtyWIP   from @tbl1 
 
 
select * from @tbl2

我自己解决了,谢谢大家的支持

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2022-01-17
    • 2017-04-16
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    相关资源
    最近更新 更多