-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpginit.sql
More file actions
66 lines (59 loc) · 2.12 KB
/
pginit.sql
File metadata and controls
66 lines (59 loc) · 2.12 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
CREATE TABLE forms (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE form_data(
id SERIAL PRIMARY KEY,
form_id INTEGER REFERENCES forms(id),
data JSONB NOT NULL
);
INSERT INTO forms (name) VALUES
('user_form'),
('survey_form'),
('order_form');
INSERT INTO form_data (form_id, data) VALUES
(1, '{"username": "alice", "email": "alice@example.com", "age": 30}'),
(1, '{"username": "bob", "email": "bob@example.com", "age": 25}'),
(1, '{"username": "charlie", "email": "charlie@example.com", "age": 35}'),
(1, '{"username": "diana", "email": "diana@example.com", "age": 28}'),
(1, '{"username": "eve", "email": "eve@example.com", "age": 32}'),
(2, '{"question1": "Great", "question2": "Yes", "rating": 5}'),
(2, '{"question1": "Good", "question2": "No", "rating": 4}'),
(2, '{"question1": "Okay", "question2": "Maybe", "rating": 3}'),
(2, '{"question1": "Poor", "question2": "No", "rating": 2}'),
(2, '{"question1": "Excellent", "question2": "Yes", "rating": 5}'),
(3, '{"product": "Widget", "quantity": 10, "price": 100}'),
(3, '{"product": "Gadget", "quantity": 5, "price": 250}'),
(3, '{"product": "Gizmo", "quantity": 20, "price": 50}'),
(3, '{"product": "Thingamajig", "quantity": 15, "price": 75}'),
(3, '{"product": "Doohickey", "quantity": 8, "price": 150}');
CREATE VIEW user_form_data AS
SELECT
fd.id,
fd.form_id,
fd.data->>'username' AS username,
fd.data->>'email' AS email,
(fd.data->>'age')::INT AS age
FROM form_data fd
JOIN forms f ON fd.form_id = f.id
WHERE f.name = 'user_form';
CREATE VIEW survey_form_data AS
SELECT
fd.id,
fd.form_id,
fd.data->>'question1' AS question1,
fd.data->>'question2' AS question2,
(fd.data->>'rating')::INT AS rating
FROM form_data fd
JOIN forms f ON fd.form_id = f.id
WHERE f.name = 'survey_form';
CREATE VIEW order_form_data AS
SELECT
fd.id,
fd.form_id,
fd.data->>'product' AS product,
(fd.data->>'quantity')::INT AS quantity,
(fd.data->>'price')::INT AS price
FROM form_data fd
JOIN forms f ON fd.form_id = f.id
WHERE f.name = 'order_form';