I can't really wrap my mind around how to do this. I have a table which is formatted as SCD2. I want to figure out how many days a given SKU had inventory. In this case, zero/non-zero matters but the amount does not. So from the 7th until the 15th it had zero stock. Again from the 17th until the 25th it had zero stock. In total 15/28 days the item was out of stock.
Unfortunately the max datetime doesn't actually tell me what day that month ends on. I also don't know how to count days within a list of datetimes. Can anyone point me in the right direction? This is what the dataset looks like:
CREATE TABLE #SCD2_TEMP ( id INTEGER , dateadded DATETIME , dateexpired DATETIME , sku NVARCHAR(MAX) , qty INTEGER ); INSERT INTO #SCD2_TEMP (id, dateadded, dateexpired, sku, qty) VALUES ('1','2019-01-30 11:11:05.000', '2019-02-07 14:38:44.007', 'ProductNumber001', '5') , ('2','2019-02-07 12:38:44.007', '2019-02-15 14:38:44.007', 'ProductNumber001', '0') , ('3','2019-02-15 14:38:44.007', '2019-02-17 19:24:52.753', 'ProductNumber001', '47') , ('4','2019-02-17 19:24:52.797', '2019-02-25 22:35:58.110', 'ProductNumber001', '0') , ('5','2019-02-25 22:35:58.147', '5000-01-01 00:00:00.000', 'ProductNumber001', '43'); SELECT * FROM #SCD2_TEMP