| | 1 | = Store with highest revenue growth in the last calendar year |
| | 2 | {{{#!sql |
| | 3 | CREATE OR REPLACE FUNCTION get_store_with_highest_revenue_growth() |
| | 4 | RETURNS TABLE ( |
| | 5 | store_id INT, |
| | 6 | store_name TEXT, |
| | 7 | previous_year_revenue NUMERIC, |
| | 8 | last_year_revenue NUMERIC, |
| | 9 | revenue_growth NUMERIC |
| | 10 | ) |
| | 11 | LANGUAGE plpgsql |
| | 12 | AS $$ |
| | 13 | BEGIN |
| | 14 | RETURN QUERY |
| | 15 | WITH yearly_revenue AS ( |
| | 16 | SELECT |
| | 17 | s.store_id, |
| | 18 | s.name AS store_name, |
| | 19 | EXTRACT(YEAR FROM o.last_modified_date)::INT AS year, |
| | 20 | SUM( |
| | 21 | p.price |
| | 22 | * o.quantity |
| | 23 | * (1 - COALESCE(o.discount, 0) / 100.0) |
| | 24 | ) AS total_revenue |
| | 25 | FROM store s |
| | 26 | JOIN sells se |
| | 27 | ON s.store_id = se.store_id |
| | 28 | JOIN product p |
| | 29 | ON se.code = p.code |
| | 30 | JOIN includes i |
| | 31 | ON p.code = i.code |
| | 32 | JOIN "order" o |
| | 33 | ON i.order_num = o.order_num |
| | 34 | WHERE o.last_modified_date >= |
| | 35 | date_trunc('year', CURRENT_DATE) - INTERVAL '2 years' |
| | 36 | AND o.last_modified_date < |
| | 37 | date_trunc('year', CURRENT_DATE) |
| | 38 | GROUP BY |
| | 39 | s.store_id, |
| | 40 | s.name, |
| | 41 | EXTRACT(YEAR FROM o.last_modified_date) |
| | 42 | ), |
| | 43 | revenue_comparison AS ( |
| | 44 | SELECT |
| | 45 | store_id, |
| | 46 | store_name, |
| | 47 | MAX( |
| | 48 | CASE |
| | 49 | WHEN year = EXTRACT(YEAR FROM CURRENT_DATE)::INT - 2 |
| | 50 | THEN total_revenue |
| | 51 | ELSE 0 |
| | 52 | END |
| | 53 | ) AS previous_year_revenue, |
| | 54 | MAX( |
| | 55 | CASE |
| | 56 | WHEN year = EXTRACT(YEAR FROM CURRENT_DATE)::INT - 1 |
| | 57 | THEN total_revenue |
| | 58 | ELSE 0 |
| | 59 | END |
| | 60 | ) AS last_year_revenue |
| | 61 | FROM yearly_revenue |
| | 62 | GROUP BY |
| | 63 | store_id, |
| | 64 | store_name |
| | 65 | ) |
| | 66 | SELECT |
| | 67 | store_id, |
| | 68 | store_name, |
| | 69 | previous_year_revenue, |
| | 70 | last_year_revenue, |
| | 71 | last_year_revenue - previous_year_revenue AS revenue_growth |
| | 72 | FROM revenue_comparison |
| | 73 | ORDER BY revenue_growth DESC |
| | 74 | LIMIT 1; |
| | 75 | END; |
| | 76 | $$; |
| | 77 | |
| | 78 | }}} |
| | 79 | |
| | 80 | == Relational Algebra |
| | 81 | - P(code, price, availability, description, ...) |
| | 82 | - O(order_num, quantity, status, last_modified_date, payment_method, discount) |
| | 83 | - I(code, order_num) |
| | 84 | - S(store_id, name, date_of_founding, physical_address, store_email, rating) |
| | 85 | - SE(code, store_id, quantity, discount) |
| | 86 | |
| | 87 | **JOIN stores with their products:** |
| | 88 | - J1 ← S ⨝S.store_id = SE.store_id SE |
| | 89 | - J2 ← J1 ⨝SE.code = P.code P |
| | 90 | |
| | 91 | **JOIN products with orders:** |
| | 92 | - J3 ← J2 ⨝P.code = I.code I |
| | 93 | - J4 ← J3 ⨝I.order_num = O.order_num O |
| | 94 | |
| | 95 | **SELECT orders from the last TWO calendar years:** |
| | 96 | - F1 ← σ last_modified_date ≥ START_OF_CURRENT_YEAR - 2 YEARS |
| | 97 | ∧ |
| | 98 | last_modified_date < START_OF_CURRENT_YEAR |
| | 99 | (J4) |
| | 100 | |
| | 101 | **Calculate revenue for each induvidual order:** |
| | 102 | - R1 ← πstore_id, name, YEAR(last_modified_date) → year, |
| | 103 | price × quantity × |
| | 104 | (1 - COALESCE(discount, 0) / 100) |
| | 105 | → order_revenue(F1) |
| | 106 | |
| | 107 | **Calculate total revenue for each store and year:** |
| | 108 | - R3 ← γstore_id, name; |
| | 109 | MAX(CASE WHEN year = current_year - 2 |
| | 110 | THEN total_revenue ELSE 0 END) |
| | 111 | → previous_year_revenue, |
| | 112 | |
| | 113 | MAX(CASE WHEN year = current_year - 1 |
| | 114 | THEN total_revenue ELSE 0 END) |
| | 115 | → last_year_revenue |
| | 116 | (R2) |
| | 117 | |
| | 118 | **Calculate revenue growth:** |
| | 119 | - R4 ← πstore_id, name, |
| | 120 | previous_year_revenue, |
| | 121 | last_year_revenue, |
| | 122 | last_year_revenue - previous_year_revenue |
| | 123 | → revenue_growth(R3) |
| | 124 | |
| | 125 | **Sort by revenue growth:** |
| | 126 | - R5 ← τrevenue_growth DESC(R4) |
| | 127 | |
| | 128 | **SELECT store with highest revenue growth:** |
| | 129 | - R_final ← γLIMIT 1(R5) |