| | 1 | = Monthly sales and profit per store |
| | 2 | |
| | 3 | === Description |
| | 4 | This view provides a monthly overview of sales and profit for each store by combining information from the REPORT, STORE, and exchanges_data tables. |
| | 5 | |
| | 6 | The view is intended for: |
| | 7 | |
| | 8 | - monitoring monthly store performance |
| | 9 | - financial reports |
| | 10 | - comparing stores |
| | 11 | - analyzing sales and profit trends |
| | 12 | |
| | 13 | ==== Tables covered by the view: |
| | 14 | |
| | 15 | - Report |
| | 16 | - Store |
| | 17 | - exchanges_data |
| | 18 | |
| | 19 | ==== SQL код |
| | 20 | {{{#!sql |
| | 21 | CREATE OR REPLACE VIEW vw_monthly_sales_profit AS |
| | 22 | SELECT |
| | 23 | s.store_id, |
| | 24 | s.name AS store_name, |
| | 25 | r.date, |
| | 26 | r.month_and_year, |
| | 27 | r.profit, |
| | 28 | r.overall_profit, |
| | 29 | ed.monthly_profit, |
| | 30 | ed.sales, |
| | 31 | ed.damages |
| | 32 | |
| | 33 | FROM store s |
| | 34 | JOIN report r |
| | 35 | ON r.store_id = s.store_id |
| | 36 | LEFT JOIN exchanges_data ed |
| | 37 | ON ed.store_id = r.store_id |
| | 38 | AND ed.date = r.date; |
| | 39 | |
| | 40 | }}} |
| | 41 | |
| | 42 | ==== Logic explanation |
| | 43 | **1.** The store information is retrieved from `store`. |
| | 44 | **2.** The `report` table provides the monthly profit and overall profit. |
| | 45 | **3.** The `exchanges_data` table provides monthly profit, sales, and damages. |
| | 46 | **4.** LEFT JOIN is used for `exchanges_data` so that a report is still displayed even if no corresponding data exchange record exists. |
| | 47 | **5.** Each row represents the financial information for a particular store and reporting date. |
| | 48 | |
| | 49 | ==== Reason for view |
| | 50 | This view is useful because: |
| | 51 | |
| | 52 | - It centralizes monthly financial information |
| | 53 | - It simplifies financial reporting queries |
| | 54 | - It allows easy comparison of store performance |
| | 55 | - It provides sales, profit, and damages in one result |
| | 56 | - It reduces repeated JOIN operations in the application |
| | 57 | |
| | 58 | Without this view, when doing any analitical tasks system will have to make multiple joints to get the results. |