16 | | |
| 17 | [[Image(step2.png)]] |
| 18 | * Тука иницијално се вчитуваат основните податоци за доставата и податоците за цена. |
| 19 | {{{ |
| 20 | SELECT |
| 21 | s.shipment_id, |
| 22 | p.payment_status, |
| 23 | CONCAT(e.employee_name, ' ', e.employee_surname) AS employee_full_name, |
| 24 | CONCAT(v.make, ' ', v.model, ' (', v.registration, ')') AS vehicle_info, |
| 25 | s.shipment_date, |
| 26 | Count(distinct sl.customer_id) as no_of_customers, |
| 27 | SUM(sl.wine_quantity) as wine_quantity_shipped, |
| 28 | SUM(sl.wine_base_price * sl.wine_quantity) AS shipment_base_price, |
| 29 | SUM(sl.wine_agreed_price * sl.wine_quantity) AS shipment_agreed_price, |
| 30 | COALESCE(ex.shipment_expenses, 0) as shipment_expenses, |
| 31 | (SUM(sl.wine_agreed_price * sl.wine_quantity) - SUM(sl.wine_base_price * sl.wine_quantity) - COALESCE(ex.shipment_expenses, 0)) as shipment_profit |
| 32 | FROM shipment s |
| 33 | JOIN payment p ON s.payment_id = p.payment_id |
| 34 | JOIN employee e ON s.employee_id = e.employee_id |
| 35 | JOIN vehicle v ON s.vehicle_id = v.vehicle_id |
| 36 | JOIN shipment_load sl ON s.shipment_id = sl.shipment_id |
| 37 | JOIN ( SELECT shipment_id, SUM(amount) AS shipment_expenses FROM shipment_has_expense_of_expense_type GROUP BY shipment_id ) ex ON s.shipment_id = ex.shipment_id |
| 38 | WHERE s.shipment_id = 5 |
| 39 | GROUP BY |
| 40 | s.shipment_id, |
| 41 | p.payment_status, |
| 42 | e.employee_name, |
| 43 | e.employee_surname, |
| 44 | v.make, |
| 45 | v.model, |
| 46 | v.registration, |
| 47 | s.shipment_date, |
| 48 | ex.shipment_expenses; |
| 49 | }}} |