| 6 | |
| 7 | create view podatoci_klienti as |
| 8 | |
| 9 | SELECT DISTINCT u.user_name, u.surname, |
| 10 | CASE WHEN tabela.quarter=1 THEN tabela.kupeni_proizvodi ELSE 0 END AS prv_kvartal, |
| 11 | CASE WHEN tabela.quarter=2 THEN tabela.kupeni_proizvodi ELSE 0 END AS vtor_kvartal, |
| 12 | CASE WHEN tabela.quarter=3 THEN tabela.kupeni_proizvodi ELSE 0 END AS tret_kvartal, |
| 13 | CASE WHEN tabela.quarter=4 THEN tabela.kupeni_proizvodi ELSE 0 END AS cetvrt_kvartal, |
| 14 | |
| 15 | CASE WHEN tabela.quarter=1 THEN tabela.suma ELSE 0 END AS prv_kvartal_suma, |
| 16 | CASE WHEN tabela.quarter=1 THEN tabela.suma ELSE 0 END AS vtor_kvartal_suma, |
| 17 | CASE WHEN tabela.quarter=1 THEN tabela.suma ELSE 0 END AS tret_kvartal_suma, |
| 18 | CASE WHEN tabela.quarter=1 THEN tabela.suma ELSE 0 END AS cetvrt_kvartal_suma |
| 19 | |
| 20 | FROM shopping_bag AS sb |
| 21 | JOIN client AS c ON sb.user_id = c.user_id |
| 22 | JOIN users AS u ON c.user_id = u.user_id |
| 23 | JOIN orders AS o ON sb.shopping_id = o.shopping_id |
| 24 | JOIN product_in_store AS pis ON o.product_in_store_id = pis.product_in_store_id |
| 25 | JOIN price AS pr ON pis.product_in_store_id = pr.product_in_store_id |
| 26 | JOIN ( |
| 27 | |
| 28 | SELECT DISTINCT c2.user_id, extract(quarter FROM sb2.order_date) AS quarter, sum(pr.price) AS suma, |
| 29 | count(sb2.shopping_id) AS kupeni_proizvodi |
| 30 | FROM shopping_bag AS sb2 |
| 31 | JOIN orders AS o ON sb2.shopping_id = o.shopping_id |
| 32 | JOIN product_in_store AS pis ON o.product_in_store_id = pis.product_in_store_id |
| 33 | join price as pr on pis.product_in_store_id=pr.product_in_store_id |
| 34 | JOIN client AS c2 ON c2.user_id = sb2.user_id |
| 35 | GROUP BY 1,2 |
| 36 | ) AS tabela ON c.user_id = tabela.user_id |
| 37 | }}} |
| 38 | |
| 39 | == Извештаи за секоја продавница, бројот на производи во неа, вкупната вредност на производите, просечна цена по производ и вкупно направени нарачки во последните 3 месеци |
| 40 | |
| 41 | {{{#!sql |
| 42 | |
| 43 | create view podatoci_prodavnici as |
| 44 | |
| 111 | |
| 112 | == Листање на сите продукти според достапност, каталог и продавница, и прикажување на продукт со најниска цена |
| 113 | {{{#!sql |
| 114 | create view produkti_cena as |
| 115 | |
| 116 | select p.names, p.in_store, pis.product_in_store_id, c.catalogue_id, c.type_, os.web_address, pr.price |
| 117 | from product as p |
| 118 | join product_in_store as pis on p.product_id=pis.product_id |
| 119 | join existss as e on pis.product_in_store_id=e.product_in_store_id |
| 120 | join catalogue as c on e.catalogue_id=c.catalogue_id |
| 121 | join online_stores as os on e.store_id=os.store_id |
| 122 | join price as pr on pis.product_in_store_id=pr.product_in_store_id |
| 123 | |
| 124 | group by 1, 2, 3, 4, 5, 6, 7; |
| 125 | |
| 126 | select pc.names, pc.in_store, pc.product_in_store_id, pc.catalogue_id, pc.type_, pc.web_address, pc.price as najniska_cena |
| 127 | from produkti_cena as pc |
| 128 | where pc.price = (select min(price) from produkti_cena)create view produkti_cena as |
| 129 | }}} |
| 130 | |
| 131 | |