-1

I am maintaining an inventory in azure sql DW and want to calculate initial and final stock of a day. Final stock of previous day will be initial stock for present day and Final stock for present day will be initial stock+Produced-sold. I have details of Produced and Sold quantities. How can I calculate initial and final quantities.

I am able to use lag function, but cant add the calculated value to get the finalqunatity in same query.

The query i wany to write but doestnt work is

Select lag(finalquantity,1) over ( Partition by Productid Order by Date) as Initialqty, (Initialqty+Produced-sold) as finalquantity from table

The finalquantity should come as a initialquantity for next record. I want this to happen recursively.

enter image description here

enter image description here

Abhinaya
  • 3
  • 1
  • 2
    Hard to answer the question without table definitions (relevant columns/indexes only), the query you've written, and the data either in the text or part of sample code we could run. –  Jul 24 '20 at 17:05
  • The query i wany to write but doestnt work is Select lag(finalquantity,1) over ( Partition by Productid Order by Date) as Initialqty, (Initialqty+Produced-sold) as finalquantity from table. The finalquantity should come as a initialquantity for next record. I want this to happen recursively. – Abhinaya Jul 24 '20 at 17:21
  • Please update the question with the information from your comment. –  Jul 24 '20 at 18:43
  • 1
    Welcome to Stack Overflow. You will have a better experience here if you take the [Tour](https://stackoverflow.com/tour) and read through [How To Ask](https://stackoverflow.com/help/how-to-ask), then write your questions with the details needed to create [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). See [How to post a T-SQL question on a public forum](https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) for tips. – Eric Brandt Jul 24 '20 at 19:44

1 Answers1

0

My answer is directly based up on the second query in the Recursive CTEs section of this answer.

I created an Inventory table as I understood your situation. I am basing my solution around InventoryDate, rather than day of week. The final item I want to mention is that your Initial stock number is coming from the Final stock number of the previous week. However, that must be defined somewhere. I hard-coded it is as the anchor member of the recursive CTE. You may pass it in as parameter or get it using a sub-query.

CREATE TABLE Inventory
(
    InventoryDate DATE
  , Produced INT
  , Sold INT
);

INSERT INTO Inventory
(
    InventoryDate
  , Produced
  , Sold
)
VALUES
  ('2020-07-20', 10,  8)
, ('2020-07-21',  9,  7)
, ('2020-07-22', 11, 10)
, ('2020-07-23', 10, 10)
, ('2020-07-24',  9,  8)
, ('2020-07-25',  7,  8)
, ('2020-07-27', 12, 14);

WITH y AS
(
    SELECT
         rn = ROW_NUMBER() OVER (ORDER BY InventoryDate)
       , InventoryDate
       , Produced
       , Sold
    FROM Inventory
)
   , x AS
(
    SELECT
         CONVERT(BIGINT, 0)                   AS [rn]
       , DATEADD(DAY, -1, MIN(InventoryDate)) AS [IventoryDate]
       , NULL                                 AS [Initial]
       , NULL                                 AS [Produced]
       , NULL                                 AS [Sold]
       , 80                                   AS [Final]
    FROM Inventory
    UNION ALL
    SELECT
               y.rn
             , y.InventoryDate
             , x.Final                       AS [Initial]
             , y.Produced
             , y.Sold
             , x.Final + y.Produced - y.Sold AS [Final]
    FROM       x
    INNER JOIN y ON y.rn = x.rn + 1
)
SELECT 
     x.rn
   , x.IventoryDate
   , x.Initial
   , x.Produced
   , x.Sold
   , x.Final
FROM x;

Here is the demo of this code.

Isaac
  • 3,240
  • 2
  • 24
  • 31
  • Thank you so much! I tried this and I get an error “Recursive common table expressions are not supported in this version.” I am using Azure SQL DW :( Is there any other way you can suggest? – Abhinaya Jul 25 '20 at 09:18
  • 1
    You should have included the fact that you are using Azure SQL DW in your question explicitly or as a tag. Perhaps one of the other approaches mentioned in the answer on which I based my answer could be adapted to work for you. However, I have no experience with or access to Azure SQL DW so I am afraid I cannot assist any further. – Isaac Jul 25 '20 at 12:51