| 3 | === **Извештаи за клиент според тромесечие, купени производи и потрошена сума** |
| 4 | {{{#!sql |
| 5 | SELECT DISTINCT u.korisnicko_ime, |
| 6 | CASE WHEN tabela.quarter=1 THEN tabela.kupeni_proizvodi ELSE 0 END AS prv_kvartal, |
| 7 | CASE WHEN tabela.quarter=2 THEN tabela.kupeni_proizvodi ELSE 0 END AS vtor_kvartal, |
| 8 | CASE WHEN tabela.quarter=3 THEN tabela.kupeni_proizvodi ELSE 0 END AS tret_kvartal, |
| 9 | CASE WHEN tabela.quarter=4 THEN tabela.kupeni_proizvodi ELSE 0 END AS cetvrt_kvartal, |
| 10 | |
| 11 | CASE WHEN tabela.quarter=1 THEN tabela.suma ELSE 0 END AS prv_kvartal_suma, |
| 12 | CASE WHEN tabela.quarter=2 THEN tabela.suma ELSE 0 END AS vtor_kvartal_suma, |
| 13 | CASE WHEN tabela.quarter=3 THEN tabela.suma ELSE 0 END AS tret_kvartal_suma, |
| 14 | CASE WHEN tabela.quarter=4 THEN tabela.suma ELSE 0 END AS cetvrt_kvartal_suma |
| 15 | |
| 16 | FROM naracki as n |
| 17 | JOIN kupuvaci as k on k.korisnicko_ime=n.korisnicko_ime |
| 18 | JOIN korisnici AS u ON k.korisnicko_ime = u.korisnicko_ime |
| 19 | JOIN parce_e_del_od_naracki as pdm on pdm.id_naracka=n.id_naracka |
| 20 | join parce as p on pdm.id_parce =p.id_parce |
| 21 | join proizvodi as pro on p.id_proizvod=pro.id_proizvod |
| 22 | JOIN cena AS c ON c.id_proizvod=pro.id_proizvod |
| 23 | JOIN ( |
| 24 | |
| 25 | SELECT DISTINCT c2.korisnicko_ime, extract(quarter FROM n.data_na_naracka) AS quarter, sum(n.suma) AS suma, |
| 26 | count(n.id_naracka) AS kupeni_proizvodi |
| 27 | FROM naracki AS n |
| 28 | JOIN parce_e_del_od_naracki as pdm on pdm.id_naracka=n.id_naracka |
| 29 | join parce as p on pdm.id_parce =p.id_parce |
| 30 | join proizvodi as pro on p.id_proizvod=pro.id_proizvod |
| 31 | JOIN cena AS c ON c.id_proizvod=pro.id_proizvod |
| 32 | JOIN kupuvaci AS c2 ON c2.korisnicko_ime = n.korisnicko_ime |
| 33 | GROUP BY 1,2 |
| 34 | ) AS tabela ON u.korisnicko_ime = tabela.korisnicko_ime |
| 35 | }}} |