CTE - 递归更新数量直到消耗总量 [英] CTE - recursively update quantity until total consumed

查看:26
本文介绍了CTE - 递归更新数量直到消耗总量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 CTE,试图确定是否可以使用订单数量递归更新库存数量记录,直到订单数量被消耗.

I've been researching CTEs trying to determine if it's possible to recursively update inventory quantity records with an order quantity until the order quantity is consumed.

以下是表格和记录:

CREATE TABLE [dbo].[myOrder](
  [Account] [float] NOT NULL,
  [Item] [float] NOT NULL,
  [Quantity] [float] NOT NULL
) ON [PRIMARY]

insert into dbo.myOrder values (12345, 1, 50)

CREATE TABLE [dbo].[myInventory](
  [ID] [int] IDENTITY(1,1) NOT NULL,
  [Account] [float] NOT NULL,
  [InvDate] [numeric](18, 0) NOT NULL,
  [Item] [float] NOT NULL,
  [Quantity] [float] NOT NULL,
  [QuantitySold] [float] NOT NULL
) ON [PRIMARY]

insert into dbo.myInventory values (12345, 111287, 1, 45, 40)
insert into dbo.myInventory values (12345, 111290, 1, 40, 0)
insert into dbo.myInventory values (12345, 111290, 1, 12, 0)
insert into dbo.myInventory values (12345, 111291, 1, 25, 0)

myOrder 表中的记录表明要为帐户 12345 为项目 #1,数量 50 创建一个订单:

The record in the myOrder table indicates that an order is to be created for account 12345 for item #1, quantity 50:

Account Item Quantity 
------- ---- --------
12345   1    50

库存表显示我们手头有大量商品 12345 用于帐户 12345:

The inventory table shows that we have plenty of item #1 on hand for account 12345:

ID Account InvDate Item Quantity QuantitySold
-- ------- ------- ---- -------- ------------
1  12345   111287  1    45       40
2  12345   111290  1    40       0
3  12345   111290  1    12       0
4  12345   111291  1    25       0

目标是开始将 50 件的订单数量插入库存记录,直到所有 50 件都用完.库存记录按 InvDate 列中的值排序.记录 1 有 5 个剩余数量 (45 - 40 = 5),这将使我们还有 45 个要为订单消费.记录 2 可以消耗 40.记录 3 可以消耗最后 5.当查询完成时,库存记录将如下所示:

The goal is to start plugging in the order quantity of 50 into the inventory records until all 50 are consumed. Inventory records are ordered by the value in the InvDate column. Record 1 has 5 remaining quantity (45 - 40 = 5), which would leave us with 45 more to consume for the order. Record 2 can consume 40. Record 3 can consume the last 5. When the query completes the inventory records would look like this:

ID Account InvDate Item Quantity QuantitySold
-- ------- ------- ---- -------- ------------
1  12345   111287  1    45       45
2  12345   111290  1    40       40
3  12345   111290  1    12       5
4  12345   111291  1    25       0

注意:库存表存储 QuantitySold,而不是 QuantityRemaining,因此您必须进行数学计算(数量 - QuantitySold)以确定每个库存记录剩余多少数量.

Note: The inventory table stores QuantitySold, not QuantityRemaining, so you have to do the math (Quantity - QuantitySold) to determine how much quantity remains per inventory record.

我在 CTE 方面几乎一无所获.我找到了很多例子来做选择,你的 CTE 有 2 个部分——初始化部分和递归部分联合在一起.我可以用游标写这个,但我认为用 CTE 是可能的,我想学习如何.

I've gotten almost nowhere with the CTE. I've found plenty of examples for doing selects where you have 2 parts to your CTE - an initialization part and the recursive part UNIONed together. I could write this with a cursor, but I think it's possible to do with a CTE and I'd like to learn how.

如果有人可以通过 CTE 确认这是可能的或解释如何设置 CTE,我将不胜感激.谢谢!

If anyone can confirm this is possible with a CTE or explain how to set up the CTE, I'd appreciate it. Thanks!

推荐答案

--@inserted table mimics inserted virtual table from AFTER INSERT triggers on [dbo].[myOrder] table
DECLARE @inserted TABLE 
(
  [Account] [float] NOT NULL,
  [Item] [float] NOT NULL,
  [Quantity] [float] NOT NULL
);

INSERT  @inserted 
VALUES  (12345, 1, 50);

WITH CteRowNumber
AS
(
    SELECT   inv.ID
            ,inv.Account
            ,inv.Item
            ,inv.Quantity
            ,inv.QuantitySold
            ,i.Quantity QuantityOrdered
            ,ROW_NUMBER() OVER(PARTITION BY inv.Account,inv.Item ORDER BY inv.ID ASC) RowNumber
    FROM    myInventory inv
    INNER JOIN @inserted i ON inv.Account = i.Account 
    AND     inv.Item = i.Item 
    WHERE   inv.Quantity > inv.QuantitySold
),  CteRecursive
AS
(
    SELECT   a.ID
            ,a.Account
            ,a.Item
            ,a.RowNumber 
            ,CASE 
                WHEN a.Quantity - a.QuantitySold < a.QuantityOrdered THEN a.Quantity - a.QuantitySold 
                ELSE a.QuantityOrdered
            END QuantitySoldNew
            ,CASE 
                WHEN a.Quantity - a.QuantitySold < a.QuantityOrdered THEN a.Quantity - a.QuantitySold 
                ELSE a.QuantityOrdered
            END RunningTotal
    FROM    CteRowNumber a
    WHERE   a.RowNumber = 1
    UNION ALL
    SELECT   crt.ID
            ,crt.Account
            ,crt.Item
            ,crt.RowNumber
            ,CASE 
                WHEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold) < crt.QuantityOrdered THEN crt.Quantity - crt.QuantitySold
                ELSE crt.QuantityOrdered - prev.RunningTotal
            END QuantitySoldNew
            ,CASE 
                WHEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold) < crt.QuantityOrdered THEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold)
                ELSE crt.QuantityOrdered
            END RunningTotal
    FROM    CteRecursive prev
    INNER JOIN CteRowNumber crt ON prev.Account = crt.Account 
    AND     prev.Item = crt.Item 
    AND     prev.RowNumber + 1 = crt.RowNumber
    WHERE   prev.RunningTotal  < crt.QuantityOrdered
)
SELECT   cte.ID
        ,cte.Account
        ,cte.Item
        ,cte.QuantitySoldNew
FROM    CteRecursive cte;
--or CteRecursive can be used to update QuantitySold column from [dbo].[myInventory] table
--UPDATE    myInventory 
--SET       QuantitySold = inv.QuantitySold + cte.QuantitySoldNew
--FROM  myInventory inv
--INNER JOIN CteRecursive cte ON inv.ID = cte.ID;

这篇关于CTE - 递归更新数量直到消耗总量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆