Files
psp_api/prisma/migrations/20260105162935_add_stock_view/migration.sql
T

18 lines
466 B
SQL
Raw Normal View History

2026-01-07 15:25:59 +03:30
CREATE OR REPLACE VIEW Stock_Available_View AS
SELECT
sb.productId,
sb.inventoryId,
sb.quantity AS physicalQuantity,
COALESCE(SUM(sr.quantity), 0) AS reservedQuantity,
(
sb.quantity - COALESCE(SUM(sr.quantity), 0)
) AS availableQuantity
FROM
Stock_Balance sb
LEFT JOIN Stock_Reservations sr ON sr.productId = sb.productId
AND sr.inventoryId = sb.inventoryId
GROUP BY
sb.productId,
sb.inventoryId,
sb.quantity;