-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.sql
More file actions
33 lines (31 loc) · 805 Bytes
/
Copy path2.sql
File metadata and controls
33 lines (31 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
--Crie um PIVOT TABLE para saber o total vendido por grupo de produto por mês referente a um determinado ano;
select * from crosstab(
$$
select pg.name,
ano.mes,
coalesce (sum(si.quantity) filter (where to_char(s.date, 'mm')::integer = ano.mes), 0) as total
from product_group pg
inner join product p on p.id_product_group=pg.id
inner join sale_item si on p.id=si.id_product
inner join sale s on si.id_sale =s.id
cross join (select * from generate_series(1,12) mes) ano
where
date_part('year', s.date)= 2019
group by 1, 2
order by 1, 2
$$,
$$
select * from generate_series(1,12) order by 1
$$) as (name varchar,
JAN numeric,
FEV numeric,
MAR numeric,
ABR numeric,
MAI numeric,
JUN numeric,
JUL numeric,
AGO numeric,
SET_ numeric,
OUT_ numeric,
NOV numeric,
DEZ numeric)