按交易日期获取先前交易的总物料数量,并获取当前总物料数量

时间:2019-07-19 06:40:11

标签: mysql multiple-tables inventory-management

我有3个表,分别是: 库存:

|ID|ItemID|TransactionDate     |Item             |Unit|Quantity|
----------------------------------------------------------------
|1 |1     |2019-07-10 12:23:51 |Plastic Cup 22oz |Pc  |200     |
----------------------------------------------------------------
|2 |2     |2019-07-09 01:23:51 |Plastic Cup 16oz |Pc  |100     |
----------------------------------------------------------------
|3 |1     |2019-07-10 01:23:51 |Plastic Cup 22oz |Pc  |100     |
----------------------------------------------------------------
|4 |3     |2019-07-09 01:23:51 |Lemon            |Pc  |100     |
----------------------------------------------------------------
|5 |2     |2019-07-10 01:23:51 |Plastic Cup 16oz |Pc  |100     |
----------------------------------------------------------------
|6 |1     |2019-07-09 01:23:51 |Plastic Cup 22oz |Pc  |100     |
----------------------------------------------------------------
|7 |3     |2019-07-10 01:23:51 |Lemon            |Pc  |100     |
----------------------------------------------------------------

消费产品:

|ID|TID|TransactionDate     |ItemID|Item             |Unit|Quantity|
------------------------------------------------------------------
|1 |1  |2019-07-10 12:23:51 |1     |Plastic Cup 22oz |Pc  |1     |
------------------------------------------------------------------
|2 |1  |2019-07-09 01:23:51 |3     |Lemon            |Pc  |1     |
------------------------------------------------------------------
|3 |2  |2019-07-10 01:23:51 |2     |Plastic Cup 16oz |Pc  |1     |
------------------------------------------------------------------
|4 |2  |2019-07-09 01:23:51 |3     |Lemon            |Pc  |1     |
------------------------------------------------------------------
|5 |3  |2019-07-10 01:23:51 |2     |Plastic Cup 16oz |Pc  |1     |
------------------------------------------------------------------

损坏的产品:

|ID|TransactionDate     |ItemID|Item             |Unit|Quantity|
----------------------------------------------------------------
|1 |2019-07-10 12:23:51 |1     |Plastic Cup 22oz |Pc  |10      |
----------------------------------------------------------------
|2 |2019-07-9 01:23:51 |2     |Plastic Cup 16oz |Pc  |10      |
----------------------------------------------------------------
|3 |2019-07-10 01:23:51 |1     |Plastic Cup 22oz |Pc  |5       |
----------------------------------------------------------------
|4 |2019-07-10 01:23:51 |3     |Lemon            |Pc  |6       |
----------------------------------------------------------------

目标输出: 可以说当前日期是(2019-07-10)

  1. Prev Bal =库存-(估计+损坏)“从当前日期之前的日期开始”
  2. 今天交付的物品(反映在“库存”表上)
  3. 当前消耗(反映在“ ConsumedProducts”表中)
  4. 当前损坏(反映在“损坏的产品”表上)
  5. 剩余余额=(先前的Bal +今天交付的物品)-(当前消耗量+当前损坏
  

|ID|ItemID|Item|Prev Bal|Current Delivered|Current Consumed|Current Damaged|Balance| --------------------------------------------------------------------------------------

我以前曾问过如何在此论坛上汇总表,以某种方式我能够获得良好的结果,这要归功于该社区的帮助,但我最近遇到了如上所述的问题。

Select I.ID, I.Item,
                    (Select IFNULL(SUM( (SELECT Quantity FROM inventory WHERE DATE(ItemTransactionDate) < CURDATE() GROUP BY I.ItemID)),0) - 
                    IFNULL((SELECT Quantity FROM inventory WHERE DATE(ItemTransactionDate) < CURDATE() GROUP BY I.ItemID),0) - 
                    IFNULL((SELECT Quantity FROM consumeditemmonitoring WHERE DATE(TransactionDate) < CURDATE() GROUP BY I.ItemID),0) As NEWBALANCE From inventory INV 
                    LEFT OUTER JOIN ( Select ItemID,IFNULL(SUM(Quantity),0) As Quantity from damagedinventory group by ItemID) DMG On INV.ItemID=DMG.ItemID 
                    LEFT OUTER JOIN (Select ItemID,IFNULL(SUM(Quantity),0) As Quantity from consumeditemmonitoring group by ItemID) CSMD On INV.ItemID=CSMD.ItemID GROUP BY I.ItemID),

                    (SELECT SUM(Quantity) As CurrentDeliveries FROM inventory WHERE DATE(ItemTransactionDate) = CURDATE() GROUP BY I.ItemID),

                    IFNULL(D.Quantity,0) As damagedQTY,

                    IFNULL(C.Quantity,0) As ConsumedQTY,

                    IFNULL(SUM(I.Quantity),0) - IFNULL(D.Quantity,0) - IFNULL(C.Quantity,0) As NEWBALANCE From inventory I LEFT OUTER JOIN 
                    (Select ItemID,IFNULL(SUM(Quantity),0) As Quantity from damagedinventory group by ItemID) D On I.ItemID=D.ItemID 
                    LEFT OUTER JOIN (Select ItemID,IFNULL(SUM(Quantity),0) As Quantity from consumeditemmonitoring group by ItemID) C On I.ItemID=C.ItemID GROUP BY I.ItemID"

我是编程的新手,所以我对此确实没有足够的知识。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

希望此方法可行,或者您可以对其进行调整。 我用了滞后功能。这将向您显示该列的上一个值(另请参见前导函数here

SQL查询:

SELECT I.ID,
       I.ItemID,
       PREV_BAL,
       ITEMS_DELIVERED_TODAY,
       CURRENT_CONSUMED,
       CURRENT_DAMAGED,
       PREV_BAL + ITEMS_DELIVERED_TODAY - CURRENT_CONSUMED - CURRENT_DAMAGED
           AS REMANING_BALANCE
  FROM (SELECT I.ID,
               I.ItemID,
               (  LAG (
                      I.Quantity,
                      1)
                  OVER (PARTITION BY I.ItemID
                        ORDER BY TRUNC (TransactionDate))
                - D.prevQuantity
                - C.prevQuantity)
                   PREV_BAL,
               I.Quantity ITEMS_DELIVERED_TODAY,
               C.Quantity CURRENT_CONSUMED,
               D.Quantity CURRENT_DAMAGED
          FROM inventory  I
               LEFT OUTER JOIN
               (  SELECT ItemID,
                         IFNULL (SUM (Quantity), 0)   AS Quantity,
                         IFNULL (SUM (prevQuantity), 0) AS prevQuantity
                    FROM (SELECT ItemID,
                                 Quantity,
                                 LAG (
                                     Quantity,
                                     1)
                                 OVER (PARTITION BY ItemID
                                       ORDER BY TRUNC (TransactionDate))
                                     prevQuantity
                            FROM damagedinventory) damagedinventory
                GROUP BY ItemID) D
                   ON I.ItemID = D.ItemID
               LEFT OUTER JOIN
               (  SELECT ItemID,
                         IFNULL (SUM (Quantity), 0)   AS Quantity,
                         IFNULL (SUM (prevQuantity), 0) AS prevQuantity
                    FROM (SELECT ItemID,
                                 Quantity,
                                 LAG (
                                     Quantity,
                                     1)
                                 OVER (PARTITION BY ItemID
                                       ORDER BY TRUNC (TransactionDate))
                                     prevQuantity
                            FROM consumeditemmonitoring) consumeditemmonitoring
                GROUP BY ItemID) C
                   ON I.ItemID = C.ItemID)