8 | | select distinct u.user_name,u.user_surname, |
9 | | case when tabela.quarter=1 then tabela.broj_kupeni else 0 end as prv_kvartal_kupeni, |
10 | | case when tabela.quarter=2 then tabela.broj_kupeni else 0 end as vtor_kvartal_kupeni, |
11 | | case when tabela.quarter=3 then tabela.broj_kupeni else 0 end as tret_kvartal_kupeni, |
12 | | case when tabela.quarter=4 then tabela.broj_kupeni else 0 end as cetvrt_kvartal_kupeni, |
13 | | ------------------------------------------------------------------------------- |
14 | | case when tabela.quarter=1 then tabela.suma else 0 end as prv_kvartal_suma, |
15 | | case when tabela.quarter=2 then tabela.suma else 0 end as vtor_kvartal_suma, |
16 | | case when tabela.quarter=3 then tabela.suma else 0 end as tret_kvartal_suma, |
17 | | case when tabela.quarter=4 then tabela.suma else 0 end as cetvrt_kvartal_suma |
18 | | |
19 | | |
20 | | from reservation as r |
21 | | join client as c on c.user_id = r.user_id |
22 | | join movieprojection as mp on mp.projection_id = r.projection_id |
23 | | join users as u on u.user_id = c.user_id |
24 | | join( |
25 | | select distinct c2.user_id,extract(quarter from r2.reservation_date) as quarter,sum(mp.projection_price) as suma, |
26 | | count(r2.reservation_id) as broj_kupeni |
27 | | from reservation as r2 |
28 | | join movieprojection as mp on mp.projection_id = r2.projection_id |
29 | | join client as c2 on c2.user_id = r2.user_id |
30 | | group by 1,2 |
31 | | ) as tabela on c.user_id = tabela.user_id |
| 8 | select |
| 9 | distinct u."name", u.surname, |
| 10 | case |
| 11 | when q1.quarter = 1 then q1.broj_kupeni |
| 12 | else 0 |
| 13 | end as prv_kvartal_kupeni, |
| 14 | case |
| 15 | when q1.quarter = 2 then q1.broj_kupeni |
| 16 | else 0 |
| 17 | end as vtor_kvartal_kupeni, |
| 18 | case |
| 19 | when q1.quarter = 3 then q1.broj_kupeni |
| 20 | else 0 |
| 21 | end as tret_kvartal_kupeni, |
| 22 | case |
| 23 | when q1.quarter = 4 then q1.broj_kupeni |
| 24 | else 0 |
| 25 | end as cetvrt_kvartal_kupeni, |
| 26 | ------------------------------------------------------------------------------- |
| 27 | case |
| 28 | when q1.quarter = 1 then q1.suma |
| 29 | else 0 |
| 30 | end as prv_kvartal_suma, |
| 31 | case |
| 32 | when q1.quarter = 2 then q1.suma |
| 33 | else 0 |
| 34 | end as vtor_kvartal_suma, |
| 35 | case |
| 36 | when q1.quarter = 3 then q1.suma |
| 37 | else 0 |
| 38 | end as tret_kvartal_suma, |
| 39 | case |
| 40 | when q1.quarter = 4 then q1.suma |
| 41 | else 0 |
| 42 | end as cetvrt_kvartal_suma |
| 43 | from |
| 44 | cart as c |
| 45 | join users as u on c.id_user_customer = u.id_user |
| 46 | join transactions as t on t.id_user_customer = c.id_user_customer |
| 47 | join |
| 48 | ( |
| 49 | select |
| 50 | distinct c2.id_user_customer, |
| 51 | extract(QUARTER |
| 52 | from |
| 53 | t2.created_at) as QUARTER, |
| 54 | sum(c2.total) as suma, |
| 55 | count(t2.id_invoice) as broj_kupeni |
| 56 | from |
| 57 | cart as c2 |
| 58 | join transactions as t2 on t2.id_cart = c2.id_cart |
| 59 | join users as u2 on u2.id_user = t2.id_user_customer |
| 60 | group by 1, 2 |
| 61 | ) as q1 |
| 62 | on u.id_user = q1.id_user_customer |