-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_postgresql.sql
73 lines (56 loc) · 1.19 KB
/
example_postgresql.sql
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
DROP TABLE IF EXISTS country CASCADE;
DROP TABLE IF EXISTS customer CASCADE;
CREATE TABLE country
(
id int primary key,
name varchar(50),
abrv char(2) null
);
CREATE TABLE customer
(
id int primary key,
name varchar(50),
male int,
countryId int references country
);
INSERT
INTO country
SELECT
id,
'Country ' || id as name,
'Ab'
FROM generate_series(0, 95) id
UNION
SELECT 96, 'Czech republic', 'Cz'
UNION
SELECT 97, 'Slovakia', 'Sk'
UNION
SELECT 98, 'Poland', 'Pl'
UNION
SELECT 99, 'Hungary', 'Hu'
UNION
SELECT 100, 'Croatia', 'Hr';
INSERT
INTO customer
SELECT id, 'Customer ' || id as name, id % 2 male, id % 101 countryId
FROM generate_series(0, 1000000) id;
SELECT co.id, co.name,
(
SELECT count(*)
FROM Customer cu
WHERE cu.countryId = co.id
) cust_cnt
FROM Country co
WHERE co.abrv IN ('Cz', 'Sk', 'Pl', 'Hu', 'Hr');
SELECT co.id, co.name, t.cust_cnt
FROM Country co
LEFT JOIN (
SELECT countryId,
count(1) cust_cnt
FROM Customer
GROUP BY countryId
) t ON co.id = t.countryId
WHERE co.abrv IN ('Cz', 'Sk', 'Pl', 'Hu', 'Hr');
create index ix_customer_countryid
on customer(countryId);
drop index ix_customer_countryid;