-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sql.sql
More file actions
128 lines (115 loc) · 4.31 KB
/
test_sql.sql
File metadata and controls
128 lines (115 loc) · 4.31 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
----------------------------------------------------------------------------------------
------------------------------- Create data model --------------------------------------
----------------------------------------------------------------------------------------
DROP SCHEMA IF EXISTS test_oplit CASCADE;
CREATE SCHEMA test_oplit;
CREATE TABLE IF NOT EXISTS test_oplit.calendar (
id serial PRIMARY KEY,
calendar_date date NOT NULL);
CREATE TABLE IF NOT EXISTS test_oplit.import (
id int PRIMARY KEY,
enabled boolean NOT NULL,
created timestamp DEFAULT NOW() NOT NULL,
import_date date NOT NULL);
CREATE TABLE IF NOT EXISTS test_oplit.production (
id serial PRIMARY KEY,
enabled boolean NOT NULL,
created timestamp DEFAULT NOW() NOT NULL,
machine_id int NOT NULL,
amount int DEFAULT 0 NOT NULL,
import_id int NOT NULL,
FOREIGN KEY (import_id) REFERENCES test_oplit.import (id)
);
----------------------------------------------------------------------------------------
------------------------------- Populate model ----------------------------------------
----------------------------------------------------------------------------------------
INSERT INTO test_oplit.calendar(calendar_date) (
SELECT date_trunc('day', dd):: date
FROM generate_series
( '2022-04-01'::timestamp
, '2022-05-01'::timestamp
, '1 day'::interval
) as gs(dd)
);
INSERT INTO test_oplit.import(enabled, id, import_date) VALUES
(TRUE, 1, '2022-04-01'),
(TRUE, 2, '2022-04-02'),
(TRUE, 3, '2022-04-03'),
(TRUE, 4, '2022-04-04'),
(TRUE, 5, '2022-04-05'),
(TRUE, 6, '2022-04-08'),
(TRUE, 7, '2022-04-09'),
(TRUE, 8, '2022-04-10'),
(TRUE, 9, '2022-04-15')
;
INSERT INTO test_oplit.production(enabled, machine_id, amount, import_id) VALUES
(TRUE, 32, floor(random() * 100 + 1)::int, 1),
(TRUE, 44, floor(random() * 100 + 1)::int, 1),
(TRUE, 78, floor(random() * 100 + 1)::int, 1),
(TRUE, 156, floor(random() * 100 + 1)::int, 1),
(TRUE, 454, floor(random() * 100 + 1)::int, 1),
(TRUE, 32, floor(random() * 100 + 1)::int, 2),
(TRUE, 78, floor(random() * 100 + 1)::int, 2),
(TRUE, 454, floor(random() * 100 + 1)::int, 2),
(TRUE, 32, floor(random() * 100 + 1)::int, 4),
(TRUE, 44, floor(random() * 100 + 1)::int, 4),
(TRUE, 78, floor(random() * 100 + 1)::int, 4),
(TRUE, 32, floor(random() * 100 + 1)::int, 7)
;
----------------------------------------------------------------------------------------
------------------------------ Production calendar -------------------------------------
----------------------------------------------------------------------------------------
CREATE TABLE test_oplit.question1 as (
SELECT
c.calendar_date as d,
p.machine_id as machine_id,
p.amount as amount
FROM test_oplit.production p
LEFT JOIN test_oplit.import i ON (
i.id = p.import_id
)
RIGHT JOIN test_oplit.calendar c ON (
c.calendar_date = i.import_date
)
ORDER BY d DESC
)
;
----------------------------------------------------------------------------------------
---------------------------- Last production for any machine ---------------------------
----------------------------------------------------------------------------------------
CREATE TABLE test_oplit.question2 as (
WITH
all_machine_ids AS (
SELECT DISTINCT machine_id as id
FROM test_oplit.production
WHERE enabled IS TRUE
),
res AS (
SELECT
c.calendar_date,
m_ids.id as machine_id,
p.amount as amount,
i.import_date as import_date,
RANK() OVER (
PARTITION BY (c.calendar_date, m_ids.id)
ORDER BY (i.import_date - c.calendar_date) DESC
) as r
FROM test_oplit.calendar c
CROSS JOIN all_machine_ids m_ids
JOIN test_oplit.production p ON (
p.machine_id = m_ids.id
)
JOIN test_oplit.import i ON (
p.import_id = i.id
AND i.import_date <= c.calendar_date
)
ORDER BY c.calendar_date DESC, r ASC
)
SELECT
calendar_date,
ARRAY_AGG('machine: ' || machine_id || ' last production is: ' || amount) production
FROM res
WHERE r = 1
GROUP BY 1
ORDER BY 1 DESC
);