| | 1 | = Customer order history |
| | 2 | |
| | 3 | === Description |
| | 4 | This view provides a complete history of orders made by customers, including order details and the products included in each order. |
| | 5 | |
| | 6 | The view is intended for: |
| | 7 | |
| | 8 | - displaying customer order history |
| | 9 | - tracking previous purchases |
| | 10 | - customer support |
| | 11 | - analyzing customer purchasing behavior |
| | 12 | |
| | 13 | ==== Tables covered by the view: |
| | 14 | |
| | 15 | - Product |
| | 16 | - Client |
| | 17 | - Order |
| | 18 | - Product |
| | 19 | - makes_order |
| | 20 | - includes |
| | 21 | |
| | 22 | ==== SQL код |
| | 23 | {{{#!sql |
| | 24 | CREATE OR REPLACE VIEW vw_customer_order_history AS |
| | 25 | SELECT |
| | 26 | c.client_id, |
| | 27 | c.first_name, |
| | 28 | c.last_name, |
| | 29 | c.email, |
| | 30 | |
| | 31 | o.order_num, |
| | 32 | o.quantity, |
| | 33 | o.status, |
| | 34 | o.last_modified_date, |
| | 35 | o.payment_method, |
| | 36 | o.discount, |
| | 37 | |
| | 38 | p.code AS product_code, |
| | 39 | p.description AS product_description, |
| | 40 | p.price |
| | 41 | |
| | 42 | FROM client c |
| | 43 | JOIN makes_order mo |
| | 44 | ON mo.client_id = c.client_id |
| | 45 | JOIN "order" o |
| | 46 | ON o.order_num = mo.order_num |
| | 47 | JOIN includes i |
| | 48 | ON i.order_num = o.order_num |
| | 49 | JOIN product p |
| | 50 | ON p.code = i.code; |
| | 51 | |
| | 52 | }}} |
| | 53 | |
| | 54 | ==== Logic explanation |
| | 55 | **1.** The `client` table provides customer information. |
| | 56 | |
| | 57 | **2.** `makes_order` connects customers with their orders. |
| | 58 | |
| | 59 | **3.** The `order` table provides order information. |
| | 60 | |
| | 61 | **4.** `includes` connects each order with its products. |
| | 62 | |
| | 63 | **5.** product` provides product information. |
| | 64 | |
| | 65 | **6.** An order containing several products produces one row for each product. |
| | 66 | |
| | 67 | ==== Reason for view |
| | 68 | This view is useful because: |
| | 69 | |
| | 70 | - It provides a centralized customer purchase history |
| | 71 | - It makes previous orders easy to retrieve |
| | 72 | - It combines customer, order, and product information |
| | 73 | - It simplifies customer support queries |
| | 74 | - It can be used for purchasing behavior analysis |
| | 75 | |
| | 76 | Without this view, every customer order-history query would have to repeat the joins between client, makes_order, order, includes, and product. |