Changes between Version 5 and Version 6 of AdvancedReports


Ignore:
Timestamp:
02/17/25 11:46:06 (5 days ago)
Author:
185022
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReports

    v5 v6  
    11= Напредни извештаи од базата (SQL и складирани процедури)
    22
    3 ==== Потрошувачка по коминтент за даден период по производ, по количина и пари
     3==== Прогноза на потрошувачка на артикли од магацинот според продажба во претходните недели, за подобра претпоставка за набавка на нови артикли
    44
    55{{{#!sql
    6 with SalesByCustomer as (select o.cust_id,
    7                                 au.art_id,
    8                                 p.price,
    9                                 count(au.unit_id)           as quantity,
    10                                 count(au.unit_id) * p.price as total_spent
    11                          from article_unit au
    12                                   join orders o on au.ord_id = o.ord_id
    13                                   join price p on au.art_id = p.art_id
    14                          where o.ord_date between '2024-01-01' and '2024-12-31'
    15                          group by o.cust_id, au.art_id, p.price)
    16 select c.cust_company_name          as customerName,
    17        a.art_name                   as articleName,
    18        sp.price                     as unitPrice,
    19        sp.quantity,
    20        sp.total_spent               as totalMoneySpent,
    21        sp.total_spent / sp.quantity as avgUnitPrice
    22 from SalesByCustomer sp
    23          join customer c on sp.cust_id = c.user_id
    24          join article a on sp.art_id = a.art_id
    25 order by c.cust_company_name, a.art_name;
    26 }}}
    27 
    28 ==== Артикли кои се при крај на залиха во магацинот и треба да се набават дополнителни количини
    29 
    30 {{{#!sql
    31 select a.art_name as articleName, w.wh_id as warehouseId, count(distinct au.unit_id) as unitNumber
     6with WeeklySales as (
     7        select a.art_id,
     8                date_trunc('week', o.ord_date) as week_start,
     9                count(*) as units_sold
     10        from orders o
     11        join article_unit au on o.ord_id = au.ord_id
     12        join unit_price up on au.unit_id = up.unit_id
     13        join price p on up.price_id = p.price_id
     14        join article a on p.art_id = a.art_id
     15        where au.wh_id = 1
     16                and o.ord_date between current_date - interval '10 weeks'
     17                        and current_date - interval '2 week'
     18        group by a.art_id, date_trunc('week',o.ord_date)
     19),
     20AvgSales as (
     21        select art_id,
     22        avg(units_sold) as avg_units_sold
     23        from WeeklySales
     24        group by art_id
     25),
     26Inventory as (
     27select a.art_id, count(distinct au.unit_id) as currentStock
    3228from article_unit au
    3329         join warehouse w on au.wh_id = w.wh_id
    34          join article a on au.art_id = a.art_id
     30         join unit_price up on au.unit_id = up.unit_id
     31         join price p on up.price_id = p.price_id
     32         join article a on p.art_id = a.art_id
    3533where w.wh_id = 1
    36 group by a.art_name, w.wh_id
    37 order by unitNumber;
     34group by a.art_id
     35order by currentStock
     36)
     37select i.art_id,
     38        a.art_name,
     39        i.currentStock,
     40        avs.avg_units_sold,
     41        (i.currentStock - avs.avg_units_sold) as remaining_after_forecast
     42from inventory i
     43join article a on i.art_id = a.art_id
     44join AvgSales avs on i.art_id = avs.art_id
    3845}}}
    3946
     
    7279{{{#!sql
    7380with ArticleSales as (
    74     select au.art_id,
     81    select a.art_id,
    7582           a.ctg_id,
    7683           w.wh_id,
     
    8087           count(au.unit_id) as unitsSold,
    8188           count(au.unit_id)*p.price as income
    82     from article_unit au
    83     join article a on au.art_id = a.art_id
     89    from article_unit au
     90    join unit_price up on up.unit_id = au.unit_id
     91    join price p on up.price_id = p.price_id
     92    join article a on p.art_id  = a.art_id
    8493    join warehouse w on au.wh_id = w.wh_id
    8594    join city c on w.city_id = c.city_id
    8695    join region r on c.region_id = r.region_id
    87     join price p on a.art_id = p.art_id
    8896    join orders o on au.ord_id = o.ord_id
    8997    where o.ord_date>=p.price_eff_date
    90     group by au.art_id, a.ctg_id, w.wh_id, c.city_id, r.region_id, p.price, p.price
     98    group by a.art_id, a.ctg_id, w.wh_id, c.city_id, r.region_id, p.price, p.price
    9199)
    92100select r.region_name as region,
     
    99107group by r.region_name, cat.ctg_name
    100108order by r.region_name, totalIncome desc;
     109
    101110}}}
    102111
     
    108117       c.city_name as warehouseCity,
    109118       r.region_name as warehouseRegion,
    110        extract(year from o.ord_date) as year,
    111        extract(quarter from o.ord_date) as quarter,
     119       extract(year from o.ord_date) as "year",
     120       extract(quarter from o.ord_date) as "quarter",
    112121       count(au.unit_id) as totalArticlesSold,
    113122       sum(o.ord_sum) as totalSales
     
    118127join orders o on au.ord_id = o.ord_id
    119128group by w.wh_id, w.wh_adr, c.city_name, r.region_name, extract(year from o.ord_date), extract(quarter from o.ord_date)
    120 order by w.wh_id,year,quarter;
     129order by w.wh_id,"year","quarter";
    121130}}}
    122131
     
    127136       c.city_name as warehouseCity,
    128137       r.region_name as warehouseRegion,
    129        extract(year from o.ord_date) as year,
     138       extract(year from o.ord_date) as "year",
    130139       sum(o.ord_sum) as totalIncome,
    131140       sum(ord_sum)-sum(au.unit_cost_price) as total_profit
     
    136145         join orders o on au.ord_id = o.ord_id
    137146group by w.wh_id, c.city_name, r.region_name, o.ord_date
    138 order by w.wh_id,year;
     147order by w.wh_id,"year";
    139148}}}
    140149
     
    142151
    143152{{{#!sql
    144 with ProductSales as (select au.art_id,
     153with ProductSales as (select a.art_id,
    145154                             a.art_name,
    146155                             cat.ctg_name                           as category,
     
    150159                             count(au.unit_id)                      as totalUnitsSold
    151160                      from article_unit au
    152                                join article a on au.art_id = a.art_id
    153                                join category cat on a.ctg_id = cat.ctg_id
    154                                join price p on a.art_id = p.art_id
    155                                join orders o on au.ord_id = o.ord_id
     161                                join unit_price up on up.unit_id = au.unit_id
     162                                join price p on up.price_id = p.price_id
     163                                join article a on p.art_id = a.art_id
     164                            join category cat on a.ctg_id = cat.ctg_id
     165                            join orders o on au.ord_id = o.ord_id
    156166                      where o.ord_date between '2024-01-01' and '2024-12-31'
    157167                        and o.ord_date >= p.price_eff_date
    158                       group by au.art_id, a.art_name, cat.ctg_name)
     168                      group by a.art_id, a.art_name, cat.ctg_name)
    159169select art_id,
    160170       art_name,
     
    166176from ProductSales
    167177order by totalProfit desc;
     178
    168179}}}