Tuesday, April 27, 2021

SQL Query - Inventory Tracking

     You can work for manufacturing company and need to track inventory adjustments from the warehouse. Some days inventory increases, on other days the inventory decreases.

Write an SQL statement that will provide a running balance of the inventory.

Source:

Expected Output:


Source Table:


Output:


Script:

WITH INVENTORY AS (
SELECT TO_DATE('28-APR-2021') INV_DATE, 100 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('29-APR-2021') INV_DATE, -50 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('30-APR-2021') INV_DATE, 75 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('01-MAY-2021') INV_DATE, -50 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('02-MAY-2021') INV_DATE, 89 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('03-MAY-2021') INV_DATE, 120 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('04-MAY-2021') INV_DATE, -150 QUANTITY_ADJ FROM DUAL
UNION ALL
SELECT TO_DATE('05-MAY-2021') INV_DATE, 40 QUANTITY_ADJ FROM DUAL)
SELECT
    inv_date,
    quantity_adj quantity_adjusted,
    SUM(quantity_adj) OVER(
    ORDER BY
        inv_date
    ) quantity_available
FROM
    inventory;








No comments:

Post a Comment