| | 1 | = Store performance overview |
| | 2 | |
| | 3 | === Description |
| | 4 | This view provides a combined overview of the overall performance of each store by bringing together store information, financial reports, sales data, and customer ratings. |
| | 5 | |
| | 6 | The view is intended for: |
| | 7 | |
| | 8 | - monitoring store performance |
| | 9 | - management dashboards |
| | 10 | - comparing stores |
| | 11 | - financial analysis |
| | 12 | - evaluating business growth |
| | 13 | |
| | 14 | ==== Tables covered by the view: |
| | 15 | |
| | 16 | - Report |
| | 17 | - Store |
| | 18 | - exchanges_data |
| | 19 | |
| | 20 | ==== SQL код |
| | 21 | {{{#!sql |
| | 22 | CREATE OR REPLACE VIEW vw_store_performance AS |
| | 23 | SELECT |
| | 24 | s.store_id, |
| | 25 | s.name AS store_name, |
| | 26 | s.date_of_founding, |
| | 27 | s.rating, |
| | 28 | |
| | 29 | COUNT(DISTINCT r.date) AS number_of_reports, |
| | 30 | COALESCE(SUM(r.profit), 0) AS total_reported_profit, |
| | 31 | COALESCE(MAX(r.overall_profit), 0) AS overall_profit, |
| | 32 | |
| | 33 | COALESCE(SUM(ed.sales), 0) AS total_sales, |
| | 34 | COALESCE(SUM(ed.damages), 0) AS total_damages, |
| | 35 | COALESCE(SUM(ed.monthly_profit), 0) AS total_monthly_profit |
| | 36 | |
| | 37 | FROM store s |
| | 38 | LEFT JOIN report r |
| | 39 | ON r.store_id = s.store_id |
| | 40 | LEFT JOIN exchanges_data ed |
| | 41 | ON ed.store_id = r.store_id |
| | 42 | AND ed.date = r.date |
| | 43 | |
| | 44 | GROUP BY |
| | 45 | s.store_id, |
| | 46 | s.name, |
| | 47 | s.date_of_founding, |
| | 48 | s.rating; |
| | 49 | |
| | 50 | }}} |
| | 51 | |
| | 52 | ==== Logic explanation |
| | 53 | **1.** The `store` table provides the basic store information and current rating. |
| | 54 | |
| | 55 | **2.** `report` provides information about store profits over different reporting periods. |
| | 56 | |
| | 57 | **3.** `exchanges_data` provides sales, damages, and monthly profit. |
| | 58 | |
| | 59 | **4.** COUNT determines how many reports exist for each store. |
| | 60 | |
| | 61 | **5.** SUM calculates the total reported profit, sales, damages, and monthly profit. |
| | 62 | |
| | 63 | **6.** MAX retrieves the highest recorded overall_profit. |
| | 64 | |
| | 65 | **7.** LEFT JOIN ensures that stores without reports or exchange records are still displayed. |
| | 66 | |
| | 67 | **8.** GROUP BY combines the information into one row per store. |
| | 68 | |
| | 69 | ==== Reason for view |
| | 70 | This view is useful because: |
| | 71 | |
| | 72 | - It provides a centralized overview of store performance |
| | 73 | - It combines financial and operational information |
| | 74 | - It makes comparison between stores easier |
| | 75 | - It simplifies management and financial reporting |
| | 76 | - It provides useful aggregated information for dashboards and analytics |
| | 77 | |
| | 78 | Without this view, management queries would need to repeatedly join and aggregate store, report, and exchanges_data, making store-performance analysis more complex and increasing the amount of SQL logic required by the application. |