| 53 | |
| 54 | - Оваа функција вади извештај според одреден месец и година за сите вина. Извештајот содржи: |
| 55 | - Име на вино |
| 56 | - Тип на вино |
| 57 | - Вкупна нарачана количина на вино од сите испораки |
| 58 | - Вкупна основна цена на вино од сите испораки |
| 59 | - Вкупна договорена цена на вино од сите испораки |
| 60 | - Вкупен профит од сите испораки |
| 61 | - Вкупен процент преметан од основната цена и договорената цена од сите испораки |
| 62 | |
| 63 | |
| 64 | {{{ |
| 65 | CREATE FUNCTION GetShippedWinesReport(month INT, year INT) |
| 66 | RETURNS TABLE( |
| 67 | WineName varchar(50), |
| 68 | WineTypeName varchar(50), |
| 69 | WineRegion varchar(50), |
| 70 | YearProduced date, |
| 71 | TotalWineBasePrice double precision, |
| 72 | TotalWineAgreedPrice double precision, |
| 73 | TotalWineQuantity bigint, |
| 74 | TotalWineProfit double precision, |
| 75 | PercentageDifference double precision |
| 76 | ) |
| 77 | LANGUAGE plpgsql |
| 78 | AS $$ |
| 79 | BEGIN |
| 80 | RETURN QUERY |
| 81 | SELECT |
| 82 | w.wine_name as WineName, |
| 83 | wt.wine_type_name AS WineTypeName, |
| 84 | wt.wine_type_region AS WineRegion, |
| 85 | w.year_produced AS YearProduced, |
| 86 | SUM(sl.wine_base_price * sl.wine_quantity) AS TotalWineBasePrice, |
| 87 | SUM(sl.wine_agreed_price * sl.wine_quantity) AS TotalWineAgreedPrice, |
| 88 | SUM(sl.wine_quantity) AS TotalWineQuantity, |
| 89 | SUM((sl.wine_agreed_price * sl.wine_quantity) - (sl.wine_base_price * sl.wine_quantity)) AS TotalWineProfit, |
| 90 | (SUM((sl.wine_agreed_price * sl.wine_quantity) - (sl.wine_base_price * sl.wine_quantity)) / SUM(sl.wine_base_price * sl.wine_quantity)) * 100 as PercentageDifference |
| 91 | FROM shipment_load sl |
| 92 | JOIN wine w ON w.wine_id = sl.wine_id |
| 93 | JOIN wine_type wt ON wt.wine_type_id = w.wine_type_id |
| 94 | JOIN shipment s ON sl.shipment_id = s.shipment_id |
| 95 | WHERE EXTRACT(MONTH FROM s.shipment_date) = month |
| 96 | AND EXTRACT(YEAR FROM s.shipment_date) = year |
| 97 | GROUP BY |
| 98 | w.wine_id, |
| 99 | w.wine_name, |
| 100 | wt.wine_type_name, |
| 101 | wt.wine_type_region, |
| 102 | w.year_produced |
| 103 | order by SUM((wine_agreed_price * wine_quantity) - (wine_base_price * wine_quantity)) desc; |
| 104 | END; |
| 105 | $$; |
| 106 | }}} |
| 107 | |
| 108 | |