| | 1 | = Stores ordered by monthly profit including monthly revenue growth |
| | 2 | {{{#!sql |
| | 3 | CREATE OR REPLACE FUNCTION get_stores_by_monthly_profit_and_revenue_growth() |
| | 4 | RETURNS TABLE ( |
| | 5 | store_id INT, |
| | 6 | store_name TEXT, |
| | 7 | month_and_year TEXT, |
| | 8 | monthly_profit NUMERIC, |
| | 9 | previous_month_revenue NUMERIC, |
| | 10 | current_month_revenue NUMERIC, |
| | 11 | revenue_growth NUMERIC |
| | 12 | ) |
| | 13 | LANGUAGE plpgsql |
| | 14 | AS $$ |
| | 15 | BEGIN |
| | 16 | RETURN QUERY |
| | 17 | WITH monthly_revenue AS ( |
| | 18 | SELECT |
| | 19 | s.store_id, |
| | 20 | s.name AS store_name, |
| | 21 | DATE_TRUNC('month', o.last_modified_date) AS month_date, |
| | 22 | SUM( |
| | 23 | p.price |
| | 24 | * o.quantity |
| | 25 | * (1 - COALESCE(o.discount, 0) / 100.0) |
| | 26 | ) AS revenue |
| | 27 | FROM store s |
| | 28 | JOIN sells se |
| | 29 | ON s.store_id = se.store_id |
| | 30 | JOIN product p |
| | 31 | ON se.code = p.code |
| | 32 | JOIN includes i |
| | 33 | ON p.code = i.code |
| | 34 | JOIN "order" o |
| | 35 | ON i.order_num = o.order_num |
| | 36 | GROUP BY |
| | 37 | s.store_id, |
| | 38 | s.name, |
| | 39 | DATE_TRUNC('month', o.last_modified_date) |
| | 40 | ), |
| | 41 | revenue_with_previous AS ( |
| | 42 | SELECT |
| | 43 | store_id, |
| | 44 | store_name, |
| | 45 | month_date, |
| | 46 | revenue, |
| | 47 | LAG(revenue) OVER ( |
| | 48 | PARTITION BY store_id |
| | 49 | ORDER BY month_date |
| | 50 | ) AS previous_month_revenue |
| | 51 | FROM monthly_revenue |
| | 52 | ) |
| | 53 | SELECT |
| | 54 | store_id, |
| | 55 | store_name, |
| | 56 | TO_CHAR(month_date, 'YYYY-MM') AS month_and_year, |
| | 57 | revenue AS monthly_profit, |
| | 58 | COALESCE(previous_month_revenue, 0) AS previous_month_revenue, |
| | 59 | revenue AS current_month_revenue, |
| | 60 | revenue - COALESCE(previous_month_revenue, 0) AS revenue_growth |
| | 61 | FROM revenue_with_previous |
| | 62 | ORDER BY |
| | 63 | monthly_profit DESC, |
| | 64 | revenue_growth DESC; |
| | 65 | END; |
| | 66 | $$; |
| | 67 | |
| | 68 | }}} |
| | 69 | |
| | 70 | == Relational Algebra |
| | 71 | - P(code, price, availability, description, ...) |
| | 72 | - O(order_num, quantity, status, last_modified_date, payment_method, discount) |
| | 73 | - I(code, order_num) |
| | 74 | - S(store_id, name, date_of_founding, physical_address, store_email, rating) |
| | 75 | - SE(code, store_id, quantity, discount) |
| | 76 | |
| | 77 | **JOIN stores with their products:** |
| | 78 | - J1 ← S ⨝S.store_id = SE.store_id SE |
| | 79 | - J2 ← J1 ⨝SE.code = P.code P |
| | 80 | |
| | 81 | **JOIN products with orders:** |
| | 82 | - J3 ← J2 ⨝P.code = I.code I |
| | 83 | - J4 ← J3 ⨝I.order_num = O.order_num O |
| | 84 | |
| | 85 | **Calculate revenue for each order and group it by store and month:** |
| | 86 | - **FORMULA:** order_revenue = price × quantity × (1 - COALESCE(discount, 0) / 100) |
| | 87 | - R1 ← γstore_id, name, YEAR(last_modified_date), |
| | 88 | MONTH(last_modified_date); |
| | 89 | Σ(order_revenue) → monthly_revenue |
| | 90 | (J4) |
| | 91 | |
| | 92 | **Calculate previous month revenue for each store:** |
| | 93 | - R2 ← γstore_id, name, month; |
| | 94 | LAG(monthly_revenue) |
| | 95 | OVER (PARTITION BY store_id ORDER BY month) |
| | 96 | → previous_month_revenue |
| | 97 | (R1) |
| | 98 | |
| | 99 | **Calculate monthly revenue growth:** |
| | 100 | - R3 ← πstore_id, name, month, |
| | 101 | monthly_revenue, |
| | 102 | previous_month_revenue, |
| | 103 | monthly_revenue - previous_month_revenue |
| | 104 | → revenue_growth |
| | 105 | (R2) |
| | 106 | |
| | 107 | **Sort by total monthly revenue and growth:** |
| | 108 | - R_final ← τmonthly_revenue DESC, |
| | 109 | revenue_growth DESC(R3) |
| | 110 | |
| | 111 | |