wiki:Напредни извештаи од базата

Version 16 (modified by 186102, 3 years ago) ( diff )

--

Просечна цена на продуктот во месец од изминатите 12 месеци

select 
sum(case when extract(month from ph.dates) =1 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =1 and now() - interval'12 months' <= ph.dates then ph.price end) as Januari, 
sum(case when extract(month from ph.dates) =2 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =2 and now() - interval'12 months' <= ph.dates then ph.price end) as Fevruari,
sum(case when extract(month from ph.dates) =3 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =3 and now() - interval'12 months' <= ph.dates then ph.price end) as Mart,
sum(case when extract(month from ph.dates) =4 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =4 and now() - interval'12 months' <= ph.dates then ph.price end) as April,

sum(case when extract(month from ph.dates) =5 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =5 and now() - interval'12 months' <= ph.dates then ph.price end) as Maj,
sum(case when extract(month from ph.dates) =6 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =6 and now() - interval'12 months' <= ph.dates then ph.price end) as Juni,
sum(case when extract(month from ph.dates) =7 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =7 and now() - interval'12 months' <= ph.dates then ph.price end) as Juli,
sum(case when extract(month from ph.dates) =8 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =8 and now() - interval'12 months' <= ph.dates then ph.price end) as Avgust,

sum(case when extract(month from ph.dates) =9 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =9 and now() - interval'12 months' <= ph.dates then ph.price end) as Septemvri,
sum(case when extract(month from ph.dates) =10 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =10 and now() - interval'12 months' <= ph.dates then ph.price end) as Oktomvri,
sum(case when extract(month from ph.dates) =11 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =11 and now() - interval'12 months' <= ph.dates then ph.price end) as Noemvri,
sum(case when extract(month from ph.dates) =12 and now() - interval'12 months' <= ph.dates then ph.price end)/count(case when extract(month from ph.dates) =12 and now() - interval'12 months' <= ph.dates then ph.price end) as Dekemvri

from project.price_history as ph
where prod_id = 3250;

Најпопуларен производ во wishlists на корисниците во изминатите 6 месеци

select p."name" as Product, p.description as Description
from project.product as p
join(
        select ptw.prod_id, count(ptw.prod_id) as cnt
        from project.product_to_wishlist as ptw
        group by ptw.prod_id
) as pw on p.prod_id = pw.prod_id
join(
                select u.time_stamp, u.product_id
                from project.updates as u
                group by u.product_id, u.time_stamp
)as u on u.product_id = p.prod_id
where now()-interval'6 months' <= u.time_stamp
group by p."name", p.description, pw.cnt, p.prod_id, u.time_stamp
order by pw.cnt desc
limit 1;

Доставувач со највеќе доставени продукти во изминатите 3 месеци

select o.distributor_id as Dostavuvac , sum(d.cnt) as Br_Dostaveni_Produkti
from project.distributor as o
join (
                select count(d.delivery_id) as cnt ,d.distributor_id, d.delivery_id
                from project.delivery as d
                where status='Delivered' AND now() - interval'3 months' <= d."date"
                group by d.delivery_id, d.distributor_id
)as d on o.distributor_id = d.distributor_id
group by o.distributor_id
order by Br_Dostaveni_Produkti desc
limit 1;
Note: See TracWiki for help on using the wiki.