Changes between Initial Version and Version 1 of AdvancedReport3


Ignore:
Timestamp:
08/21/26 05:33:40 (30 hours ago)
Author:
235018
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport3

    v1 v1  
     1= List of products that are low on stock and high in demand
     2{{{#!sql
     3CREATE OR REPLACE FUNCTION get_low_stock_high_demand_products(
     4    p_stock_threshold INT,
     5    p_demand_threshold INT
     6)
     7RETURNS TABLE (
     8    product_code INT,
     9    product_description TEXT,
     10    current_stock INT,
     11    number_of_orders BIGINT,
     12    total_quantity_sold BIGINT
     13)
     14LANGUAGE plpgsql
     15AS $$
     16BEGIN
     17    RETURN QUERY
     18    SELECT
     19        p.code AS product_code,
     20        p.description AS product_description,
     21        p.availability AS current_stock,
     22        COUNT(DISTINCT o.order_num) AS number_of_orders,
     23        COALESCE(SUM(o.quantity), 0) AS total_quantity_sold
     24    FROM product p
     25    JOIN includes i
     26        ON p.code = i.code
     27    JOIN "order" o
     28        ON i.order_num = o.order_num
     29    GROUP BY
     30        p.code,
     31        p.description,
     32        p.availability
     33    HAVING
     34        p.availability < p_stock_threshold
     35        AND COUNT(DISTINCT o.order_num) >= p_demand_threshold
     36    ORDER BY
     37        number_of_orders DESC,
     38        total_quantity_sold DESC,
     39        current_stock ASC;
     40END;
     41$$;
     42
     43}}}
     44
     45== Relational Algebra
     46- P(code, price, availability, description, ...)
     47- O(order_num, quantity, status, last_modified_date, payment_method, discount)
     48- I(code, order_num)
     49
     50**JOIN products with orders:**
     51- J1 ← P ⟕P.code = I.code I
     52- J2 ← J1 ⨝I.order_num = O.order_num O
     53
     54**Calculate demand statistics:**
     55- **FORMULA:** number_of_orders = COUNT(DISTINCT order_num) 
     56
     57- D ← γcode, description, availability;
     58     COUNT(DISTINCT order_num) → number_of_orders,
     59     Σ(quantity) → total_quantity_sold
     60     (J2)
     61
     62**Filter low-stock and high-demand products:**
     63- F ← σavailability < p_stock_threshold
     64     ∧ number_of_orders ≥ p_demand_threshold(D)
     65
     66**Sort by highest number of orders:**
     67- R_final ← τnumber_of_orders DESC,
     68           total_quantity_sold DESC,
     69           availability ASC(F)
     70
     71