Trae+SQLazy VibeCoding SQL:Calculate original inventory by working backwards from a given date

Problem description

Computing objective

Given the date 2024-02-26, work backwards from the inventory after receipt CUSTQTY on that date: for each preceding record, subtract its receipt quantity QTY to get its original inventory UPDATED_CUSTQTY. Stop at the record where the original inventory reaches 0. Leave UPDATED_CUSTQTY blank for records after the given date, and for earlier records whose original inventory has already reached 0.

Source data

Database table warehouse:

ITEM LOC NEEDDATE QTY CUSTQTY

ABC XYZ 2024-02-29 00:00:00 0.6 3

ABC XYZ 2024-02-28 00:00:00 0.6 3

ABC XYZ 2024-02-27 00:00:00 0.6 3

ABC XYZ 2024-02-26 00:00:00 0.6 3

ABC XYZ 2024-02-23 00:00:00 0.6 3

ABC XYZ 2024-02-22 00:00:00 0.6 3

ABC XYZ 2024-02-21 00:00:00 0.6 3

ABC XYZ 2024-02-20 00:00:00 0.6 3

ABC XYZ 2024-02-19 00:00:00 0.6 3

ABC XYZ 2024-02-16 00:00:00 0.6 3

ABC XYZ 2024-02-15 00:00:00 0.6 3

ABC XYZ 2024-02-14 00:00:00 0.6 3

ABC XYZ 2024-02-13 00:00:00 4.8 3

Expected result

ITEM LOC NEEDDATE QTY CUSTQTY UPDATED_CUSTQTY

ABC XYZ 2024-02-29 00:00:00 0.6 3

ABC XYZ 2024-02-28 00:00:00 0.6 3

ABC XYZ 2024-02-27 00:00:00 0.6 3

ABC XYZ 2024-02-26 00:00:00 0.6 3 2.4

ABC XYZ 2024-02-23 00:00:00 0.6 3 1.8

ABC XYZ 2024-02-22 00:00:00 0.6 3 1.2

ABC XYZ 2024-02-21 00:00:00 0.6 3 0.6

ABC XYZ 2024-02-20 00:00:00 0.6 3 0

ABC XYZ 2024-02-19 00:00:00 0.6 3

ABC XYZ 2024-02-16 00:00:00 0.6 3

ABC XYZ 2024-02-15 00:00:00 0.6 3

ABC XYZ 2024-02-14 00:00:00 0.6 3

ABC XYZ 2024-02-13 00:00:00 4.8 3

Explanation

The given date is 2024-02-26. The inventory after receipt on that day is 3 and the receipt quantity is 0.6, so its original inventory is 3-0.6=2.4.

The preceding record is 2024-02-23, whose original inventory is 2.4-0.6=1.8. Going further back the values are 1.2 and 0.6, reaching 0 on 2024-02-20.

Records on 2024-02-27 and later cannot be derived backwards from the given date, so they are left blank; for records on 2024-02-19 and earlier, the original inventory has already dropped below 0, so they are also left blank.

Write SQL with Trae

Agent prompt

Install SQLazy's SKILL (download from https://www.sqlazy.com) in Trae, then enter command:

sqlazy-planning 
Computing objective
Given the date 2024-02-26, work backwards from the inventory after receipt CUSTQTY on that date: for each preceding record, subtract its receipt quantity QTY to get its original inventory UPDATED_CUSTQTY. Stop at the record where the original inventory reaches 0. Leave UPDATED_CUSTQTY blank for records after the given date, and for earlier records whose original inventory has already reached 0. 

Source data
Database table warehouse: 
 ITEM 	 LOC 	 NEEDDATE 	 QTY 	 CUSTQTY 
 ABC 	 XYZ 	 2024-02-29 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-28 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-27 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-26 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-23 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-22 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-21 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-20 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-19 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-16 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-15 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-14 00:00:00 	 0.6 	 3 
 ABC 	 XYZ 	 2024-02-13 00:00:00 	 4.8 	 3 
 
Expected result 
 ITEM 	 LOC 	 NEEDDATE 	 QTY 	 CUSTQTY 	 UPDATED_CUSTQTY 
 ABC 	 XYZ 	 2024-02-29 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-28 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-27 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-26 00:00:00 	 0.6 	 3 	 2.4
 ABC 	 XYZ 	 2024-02-23 00:00:00 	 0.6 	 3 	 1.8
 ABC 	 XYZ 	 2024-02-22 00:00:00 	 0.6 	 3 	 1.2
 ABC 	 XYZ 	 2024-02-21 00:00:00 	 0.6 	 3 	 0.6
 ABC 	 XYZ 	 2024-02-20 00:00:00 	 0.6 	 3 	 0
 ABC 	 XYZ 	 2024-02-19 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-16 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-15 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-14 00:00:00 	 0.6 	 3 	 
 ABC 	 XYZ 	 2024-02-13 00:00:00 	 4.8 	 3 	 

Picture1png

Generate SQLazy code

Trae will return a SQLazy script located in the project's `nsql` subdirectory. You can load the test data and open the script in the SQLazy playground; first enter the command"View script in a web page" in Trae.

Picture2png

Then click the hyperlink in the returned result:

Picture3png

You can execute it step by step and observe the intermediate results at each stage.

Compile to SQL

After confirming the logic of the above steps, use the SQLazy compiler to automatically generate native SQL (MySQL syntax in this case).

WITH baseInfo AS (
  SELECT
    MAX(
      CASE
        WHEN NEEDDATE = STR_TO_DATE('2024-02-26 00:00:00', '%Y-%m-%d %H:%i:%s') THEN CUSTQTY
      END
    ) AS baseCust
  FROM
    warehouse
)
SELECT
  ITEM,
  LOC,
  NEEDDATE,
  QTY,
  CUSTQTY,
  CASE
    WHEN isAfter = 2
    AND cumQTY <= (
      SELECT
        baseCust
      FROM
        baseInfo
    ) THEN round(
      (
        SELECT
          baseCust
        FROM
          baseInfo
      ) - cumQTY,
      6
    )
    ELSE NULL
  END AS UPDATED_CUSTQTY
FROM
  (
    SELECT
      ITEM,
      LOC,
      NEEDDATE,
      QTY,
      CUSTQTY,
      CASE
        WHEN NEEDDATE > STR_TO_DATE('2024-02-26 00:00:00', '%Y-%m-%d %H:%i:%s') THEN 1
        ELSE 2
      END AS isAfter,
      SUM(QTY) OVER (
        PARTITION BY (
          CASE
            WHEN NEEDDATE > STR_TO_DATE('2024-02-26 00:00:00', '%Y-%m-%d %H:%i:%s') THEN 1
            ELSE 2
          END
        )
        ORDER BY
          NEEDDATE DESC ROWS UNBOUNDED PRECEDING
      ) AS cumQTY
    FROM
      warehouse
  ) Value_4
ORDER BY
  NEEDDATE DESC

SQLazy lets you describe the logic in business language instead of writing nested queries in SQL syntax. In this "work backwards from a given date to the original inventory" example, the core idea is one sentence: order the records from newest to oldest, accumulate the receipt quantities, and keep only the records before the original inventory drops below zero. If you hand-wrote the SQL, you would have to build SUM()OVER (ORDER BY ... ROWS UNBOUNDED PRECEDING) yourself, notice that the sort direction and the accumulation direction are opposite, handle the NULL cases after the given date and before the inventory reaches 0, and write a separate version for the date-literal syntax of every database. SQLazy breaks the whole logic into three independently verifiable steps: ordering the records (`sort`), accumulating the daily change (`compute ... cum`), and filtering the result while dropping the helper column (`derive ... delete`). Each step can be run on its own so you can inspect the intermediate result; you only need to confirm the business meaning of each step — "is the order right, is the running total right, is the cut-off point right?" — while window frames and dialect differences are handled by the compiler that generates the final executable SQL.

SQLazy Online Experience: sqlazy.com (free, no registration required)

SQLazy Repository: github.com/SPLWare/SQLazy