| | 1 | = Stores ordered by total revenue in the last calendar year from highest to lowest |
| | 2 | {{{#!sql |
| | 3 | CREATE OR REPLACE FUNCTION get_stores_by_last_calendar_year_revenue() |
| | 4 | RETURNS TABLE ( |
| | 5 | store_id INT, |
| | 6 | store_name TEXT, |
| | 7 | number_of_orders BIGINT, |
| | 8 | total_quantity_sold BIGINT, |
| | 9 | total_revenue NUMERIC |
| | 10 | ) |
| | 11 | LANGUAGE plpgsql |
| | 12 | AS $$ |
| | 13 | BEGIN |
| | 14 | RETURN QUERY |
| | 15 | SELECT |
| | 16 | s.store_id, |
| | 17 | s.name AS store_name, |
| | 18 | COUNT(DISTINCT o.order_num) AS number_of_orders, |
| | 19 | COALESCE(SUM(o.quantity), 0) AS total_quantity_sold, |
| | 20 | COALESCE( |
| | 21 | SUM( |
| | 22 | p.price |
| | 23 | * o.quantity |
| | 24 | * (1 - COALESCE(o.discount, 0) / 100.0) |
| | 25 | ), |
| | 26 | 0 |
| | 27 | ) AS total_revenue |
| | 28 | FROM store s |
| | 29 | LEFT JOIN sells se |
| | 30 | ON s.store_id = se.store_id |
| | 31 | LEFT JOIN product p |
| | 32 | ON se.code = p.code |
| | 33 | LEFT JOIN includes i |
| | 34 | ON p.code = i.code |
| | 35 | LEFT JOIN "order" o |
| | 36 | ON i.order_num = o.order_num |
| | 37 | AND o.last_modified_date >= DATE_TRUNC( |
| | 38 | 'year', |
| | 39 | CURRENT_DATE |
| | 40 | ) - INTERVAL '1 year' |
| | 41 | AND o.last_modified_date < DATE_TRUNC( |
| | 42 | 'year', |
| | 43 | CURRENT_DATE |
| | 44 | ) |
| | 45 | GROUP BY |
| | 46 | s.store_id, |
| | 47 | s.name |
| | 48 | ORDER BY |
| | 49 | total_revenue DESC; |
| | 50 | END; |
| | 51 | $$; |
| | 52 | |
| | 53 | }}} |
| | 54 | |
| | 55 | == Relational Algebra |
| | 56 | - P(code, price, availability, description, ...) |
| | 57 | - O(order_num, quantity, status, last_modified_date, payment_method, discount) |
| | 58 | - I(code, order_num) |
| | 59 | - S(store_ID, name, date_of_founding, physical_address, store_email, rating) |
| | 60 | - SE(code, store_ID, quantity, discount) |
| | 61 | |
| | 62 | |
| | 63 | **JOIN stores with products they sell:** |
| | 64 | - J1 ← S ⟕S.store_ID = SE.store_ID SE |
| | 65 | - J2 ← J1 ⟕SE.code = P.code P |
| | 66 | |
| | 67 | **JOIN products with orders:** |
| | 68 | - J3 ← J2 ⟕P.code = I.code I |
| | 69 | - J4 ← J3 ⟕I.order_num = O.order_num O |
| | 70 | |
| | 71 | **FILTER orders from the last calendar year:** |
| | 72 | - F ← σlast_modified_date ≥ start_date |
| | 73 | ∧ last_modified_date < end_date(J4) |
| | 74 | |
| | 75 | **Calculate revenue for each order:** |
| | 76 | - **FORMULA:** order_revenue = order_total × (1 - COALESCE(discount, 0) / 100) |
| | 77 | |
| | 78 | - R1 ← πstore_ID, store_name, order_num, quantity, |
| | 79 | price × quantity × |
| | 80 | (1 - COALESCE(discount, 0) / 100) |
| | 81 | → order_revenue(F) |
| | 82 | |
| | 83 | **Calculate revenue for each store:** |
| | 84 | - R2 ← γstore_ID, store_name; |
| | 85 | COUNT(DISTINCT order_num) → number_of_orders, |
| | 86 | Σ(quantity) → total_quantity_sold, |
| | 87 | Σ(order_revenue) → total_revenue |
| | 88 | (R1) |
| | 89 | - For stores without orders in the last calendar year: |
| | 90 | - number_of_orders = 0 |
| | 91 | - total_quantity_sold = 0 |
| | 92 | - total_revenue = 0 |
| | 93 | |
| | 94 | **Sort by store revenue:** |
| | 95 | - R_final ← τtotal_revenue DESC(R2) |
| | 96 | |
| | 97 | |