-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_queries.sql
More file actions
91 lines (78 loc) · 2.78 KB
/
example_queries.sql
File metadata and controls
91 lines (78 loc) · 2.78 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--set a default new query template with the command QE Boost: Set New Query Template
# Example queries
select s.id, s.name, city,
--p.name,
p.category, sum(p.volume) as total_volume from stores as s
inner join inventories as i on i.store_id = s.id
inner join products as p on p.id = i.product_id
--where s.id = 243
group by p.category, s.id, s.name, city
order by total_volume;
create or replace function category_volume(store_id int, category text) returns int as
$$
declare
vol int;
begin
select sum(i.quantity * p.volume) into vol
from stores s
inner join inventories i on (i.store_id = s.id)
inner join products p on (i.product_id = p.id)
where s.id = $1 and p.category = $2;
return vol;
end;
$$ language plpgsql;
select s.id, s.name, category_volume(s.id, 'Beer') as beer, category_volume(s.id, 'Wine') as wine, category_volume(s.id, 'Spirit') as spirit
--(select sum(i.quantity * p.volume) where p.category = 'Beer') as beer, p.category
from stores as s
--inner join inventories i on (i.store_id = s.id)
--inner join products p on (i.product_id = p.id)
--where s.id = 243
--and p.category in ('Beer', 'Wine', 'Spirit')
--group by s.id, p.category
;
select s.id, s.name, s.city,
(select sum(i.quantity * p.volume)
from inventories i
inner join products p on p.id = i.product_id
where i.store_id = s.id
and p.category = 'Beer') as beer_volume,
(select sum(i.quantity * p.volume)
from inventories i
inner join products p on p.id = i.product_id
where i.store_id = s.id and p.category = 'Wine') as wine_volume,
(select sum(i.quantity * p.volume)
from inventories i
inner join products p on p.id = i.product_id
where i.store_id = s.id and p.category = 'Spirit') as spirits_volume
from stores s
order by s.id
update stores set (beer_volume, wine_volume, spirits_volume) =
(
(select sum(i.quantity * p.volume)
from inventories i
inner join products p on p.id = i.product_id
where i.store_id = stores.id
and p.category = 'Beer')
,(select sum(i.quantity * p.volume)
from inventories i
inner join products p on p.id = i.product_id
where i.store_id = stores.id
and p.category = 'Wine')
,(select sum(i.quantity * p.volume)
from inventories i
inner join products p on p.id = i.product_id
where i.store_id = stores.id
and p.category = 'Spirit')
);
select sb.id, s.id, s.name, s.city
from subdivisions sb
inner join stores s on ST_Intersects(sb.boundary, s.location)
order by sb.id
-- subdivs and volumes
select sb.id, sum(s.beer_volume), sum(s.wine_volume), sum(s.spirits_volume)
from subdivisions sb
inner join stores s on ST_Intersects(sb.boundary, s.location)
group by sb.id
-- centre of subdiv
select st_asgeojson(st_centroid(boundary::geometry)) as centre from subdivisions
where province = 'Ontario';