Search code examples
mysqltransactionsinventory

Daily Inventory from Bought/Sold transaction log


The following SQL queries describe two tables and their contents, a small and simplified part of a big inventory system.

The first is a transaction log listing bought and sold quantities of various items.

CREATE TABLE `Inventar_actions` (
  `PartID` char(8) NOT NULL,
  `DateSeq` smallint(4) unsigned NOT NULL,
  `ActionType` enum('Bought','Sold') NOT NULL,
  `Units` bigint(20) unsigned NOT NULL,
  PRIMARY KEY (`PartID`,`DateSeq`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test`.`Inventar_actions` (`PartID`, `DateSeq`, `ActionType`, `Units`) VALUES 
('0001', '1020', 'Bought', '100'),('0001', '1025', 'Sold', '20'),('0001', '1028', 'Sold', '30'),
('0001', '1031', 'Bought', '100'),('0001', '1035', 'Sold', '50');

The second table is a calendar, listing business days and their sequential numbers from an arbitrary epoch:

CREATE TABLE `BusinessDates` (
  `DateSeq` int(10) unsigned NOT NULL,
  `BusinessDate` date NOT NULL,
  PRIMARY KEY (`DateSeq`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test`.`BusinessDates` (`DateSeq`, `BusinessDate`) VALUES 
('1015', '2020-02-03'),('1016', '2020-02-04'),('1017', '2020-02-05'),('1018', '2020-02-06'),('1019', '2020-02-07'),
('1020', '2020-02-10'),('1021', '2020-02-11'),('1022', '2020-02-12'),('1023', '2020-02-13'),('1024', '2020-02-14'),
('1025', '2020-02-17'),('1026', '2020-02-18'),('1027', '2020-02-19'),('1028', '2020-02-20'),('1029', '2020-02-21'),
('1030', '2020-02-24'),('1031', '2020-02-25'),('1032', '2020-02-26'),('1033', '2020-02-27'),('1034', '2020-02-28'),
('1035', '2020-03-02'),('1036', '2020-03-03'),('1037', '2020-03-04'),('1038', '2020-03-05'),('1039', '2020-03-06')
;

What I need is a daily inventory, one that looks something like this:

| 1020 | 2020-02-10 | 100 | Bought | 100 |
| 1021 | 2020-02-11 |     |        | 100 |
| 1022 | 2020-02-12 |     |        | 100 |
| 1023 | 2020-02-13 |     |        | 100 |
| 1024 | 2020-02-14 |     |        | 100 |
| 1025 | 2020-02-17 |  20 | Sold   |  80 |
| 1026 | 2020-02-18 |     |        |  80 |
| 1027 | 2020-02-19 |     |        |  80 |
| 1028 | 2020-02-20 |  30 | Sold   |  50 |
| 1029 | 2020-02-21 |     |        |  50 |
| 1030 | 2020-02-24 |     |        |  50 |
| 1031 | 2020-02-25 | 100 | Bought | 150 |
| 1032 | 2020-02-26 |     |        | 150 |
| 1033 | 2020-02-27 |     |        | 150 |
| 1034 | 2020-02-28 |     |        | 150 |
| 1035 | 2020-03-02 |  50 | Sold   | 100 |
| 1036 | 2020-03-03 |     |        | 100 |
| 1037 | 2020-03-04 |     |        | 100 |
| 1038 | 2020-03-05 |     |        | 100 |
| 1039 | 2020-03-06 |     |        | 100 |

That is, for each business date what is the balance of each PartID, given parts bought and sold.

I used the following query to line up dates, date-sequence and transactions:

SELECT d.DateSeq, d.BusinessDate, a.`Units`, a.`ActionType` 
FROM `BusinessDates` AS d LEFT JOIN 
(SELECT DateSeq, ActionType, Units FROM `Inventar_actions` WHERE PartID = '0001') AS a USING (DateSeq)
WHERE d.DateSeq BETWEEN 1020 AND 1039
ORDER BY d.DateSeq;

This produces the first four columns of the desired output.

Unfortunately, I couldn't find a query that will also produce the last column, the balance inventory of parts.

Please note that there are a lot of PartID's and thousands of business days.

Please help...


Solution

  • This works with mysql 5.x and 8.0

    CREATE TABLE `Inventar_actions` (
      `PartID` char(8) NOT NULL,
      `DateSeq` smallint(4) unsigned NOT NULL,
      `ActionType` enum('Bought','Sold') NOT NULL,
      `Units` bigint(20) unsigned NOT NULL,
      PRIMARY KEY (`PartID`,`DateSeq`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    INSERT INTO `Inventar_actions` (`PartID`, `DateSeq`, `ActionType`, `Units`) VALUES 
    ('0001', '1020', 'Bought', '100'),('0001', '1025', 'Sold', '20'),('0001', '1028', 'Sold', '30'),
    ('0001', '1031', 'Bought', '100'),('0001', '1035', 'Sold', '50');
    
    ✓
    
    ✓
    
    CREATE TABLE `BusinessDates` (
      `DateSeq` int(10) unsigned NOT NULL,
      `BusinessDate` date NOT NULL,
      PRIMARY KEY (`DateSeq`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    INSERT INTO `BusinessDates` (`DateSeq`, `BusinessDate`) VALUES 
    ('1015', '2020-02-03'),('1016', '2020-02-04'),('1017', '2020-02-05'),('1018', '2020-02-06'),('1019', '2020-02-07'),
    ('1020', '2020-02-10'),('1021', '2020-02-11'),('1022', '2020-02-12'),('1023', '2020-02-13'),('1024', '2020-02-14'),
    ('1025', '2020-02-17'),('1026', '2020-02-18'),('1027', '2020-02-19'),('1028', '2020-02-20'),('1029', '2020-02-21'),
    ('1030', '2020-02-24'),('1031', '2020-02-25'),('1032', '2020-02-26'),('1033', '2020-02-27'),('1034', '2020-02-28'),
    ('1035', '2020-03-02'),('1036', '2020-03-03'),('1037', '2020-03-04'),('1038', '2020-03-05'),('1039', '2020-03-06')
    ;
    
    ✓
    
    ✓
    
    SELECT 
       d.DateSeq
       , d.BusinessDate
       , a.`Units`
       , a.`ActionType`
          , 
       CASE 
         WHEN a.`ActionType` = 'Bought' THEN @inventory := @inventory + a.`Units`
       WHEN a.`ActionType` = 'Sold' THEN @inventory := @inventory - a.`Units`
       ELSE
        @inventory := @inventory
       END inventory
    FROM (SELECT * FROM `BusinessDates` ORDER BY  BusinessDate ASC) AS d LEFT JOIN 
    (SELECT DateSeq, ActionType, Units FROM `Inventar_actions` WHERE PartID = '0001') AS a USING (DateSeq)
    ,(SELECT @inventory := 0) b
    WHERE d.DateSeq BETWEEN 1020 AND 1039
    ORDER BY d.DateSeq;
    
    DateSeq | BusinessDate | Units | ActionType | inventory
    ------: | :----------- | ----: | :--------- | :--------
       1020 | 2020-02-10   |   100 | Bought     | 100      
       1021 | 2020-02-11   |  null | null       | 100      
       1022 | 2020-02-12   |  null | null       | 100      
       1023 | 2020-02-13   |  null | null       | 100      
       1024 | 2020-02-14   |  null | null       | 100      
       1025 | 2020-02-17   |    20 | Sold       | 80       
       1026 | 2020-02-18   |  null | null       | 80       
       1027 | 2020-02-19   |  null | null       | 80       
       1028 | 2020-02-20   |    30 | Sold       | 50       
       1029 | 2020-02-21   |  null | null       | 50       
       1030 | 2020-02-24   |  null | null       | 50       
       1031 | 2020-02-25   |   100 | Bought     | 150      
       1032 | 2020-02-26   |  null | null       | 150      
       1033 | 2020-02-27   |  null | null       | 150      
       1034 | 2020-02-28   |  null | null       | 150      
       1035 | 2020-03-02   |    50 | Sold       | 100      
       1036 | 2020-03-03   |  null | null       | 100      
       1037 | 2020-03-04   |  null | null       | 100      
       1038 | 2020-03-05   |  null | null       | 100      
       1039 | 2020-03-06   |  null | null       | 100      
    

    db<>fiddle here