Changes between Version 10 and Version 11 of AdvancedDatabaseDevelopment


Ignore:
Timestamp:
08/28/26 05:45:59 (10 days ago)
Author:
232012
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedDatabaseDevelopment

    v10 v11  
    104104}}}
    105105* **Implementation:** 
     106
     107{{{#!div style="text-align: justify; width: 100%;"
     108  First, we create a table where all of the "HOT" products will be inserted.
     109}}}
     110
     111{{{
     112CREATE TABLE IF NOT EXISTS project.hot_item_admin_notes (
     113    product_id BIGINT NOT NULL REFERENCES project.products(product_id) ON DELETE CASCADE,
     114    release_title VARCHAR NOT NULL,
     115    format VARCHAR NOT NULL,
     116    total_quantity_sold INTEGER NOT NULL,
     117    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
     118);
     119}}}
     120
     121{{{#!div style="text-align: justify; width: 100%;"
     122  Then, we create a procedure that will place the items into the table mentioned and shown above.
     123}}}
     124
     125{{{
     126CREATE OR REPLACE PROCEDURE project.flag_hot_items()
     127LANGUAGE plpgsql
     128AS $$
     129BEGIN
     130    INSERT INTO project.hot_item_admin_notes
     131    (
     132        product_id,
     133        release_title,
     134        format,
     135        total_quantity_sold
     136    )
     137    SELECT
     138        p.product_id,
     139        r.title,
     140        p.format,
     141        SUM(op.quantity)
     142    FROM project.order_products op
     143    JOIN project.orders o
     144        ON o.order_id = op.order_id
     145    JOIN project.products p
     146        ON p.product_id = op.product_id
     147    JOIN project.releases r
     148        ON r.release_id = p.release_id
     149    WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '7 days'
     150      AND o.status <> 'CANCELLED'
     151    GROUP BY
     152        p.product_id,
     153        r.title,
     154        p.format
     155    HAVING SUM(op.quantity) > 50
     156       AND NOT EXISTS (
     157            SELECT 1
     158            FROM project.hot_item_admin_notes hin
     159            WHERE hin.product_id = p.product_id
     160       );
     161END;
     162$$;
     163}}}
    106164
    107165== Triggers