forked from tesseract-olap/tesseract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional-cut.txt
237 lines (187 loc) · 7.38 KB
/
conditional-cut.txt
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
alpaca :) create table test_2 (manager_id UInt8, security_id UInt8, volume UInt32) ENGINE = Log
CREATE TABLE test_2
(
manager_id UInt8,
security_id UInt8,
volume UInt32
)
ENGINE = Log
Ok.
0 rows in set. Elapsed: 0.004 sec.
alpaca :) insert into test_2 values (1,1,100), (1,2,500), (1,3,700), (2,1,900),(2,4,1100)
INSERT INTO test_2 VALUES
Ok.
5 rows in set. Elapsed: 0.004 sec.
alpaca :) select * from test_2
SELECT *
FROM test_2
┌─manager_id─┬─security_id─┬─volume─┐
│ 1 │ 1 │ 100 │
│ 1 │ 2 │ 500 │
│ 1 │ 3 │ 700 │
│ 2 │ 1 │ 900 │
│ 2 │ 4 │ 1100 │
└────────────┴─────────────┴────────┘
5 rows in set. Elapsed: 0.005 sec.
alpaca :) select manager_id, groupArray(security_id) as securities, groupArray(volume) as volumes from test_2 group by manager_id
SELECT
manager_id,
groupArray(security_id) AS securities,
groupArray(volume) AS volumes
FROM test_2
GROUP BY manager_id
┌─manager_id─┬─securities─┬─volumes───────┐
│ 1 │ [1,2,3] │ [100,500,700] │
│ 2 │ [1,4] │ [900,1100] │
└────────────┴────────────┴───────────────┘
2 rows in set. Elapsed: 0.007 sec.
alpaca :) select manager_id, securities, volumes from (select manager_id, groupArray(security_id) as securities, groupArray(volume) as volumes from test_2 group by manager_id) where has(securities, 2)
SELECT
manager_id,
securities,
volumes
FROM
(
SELECT
manager_id,
groupArray(security_id) AS securities,
groupArray(volume) AS volumes
FROM test_2
GROUP BY manager_id
)
WHERE has(securities, 2)
┌─manager_id─┬─securities─┬─volumes───────┐
│ 1 │ [1,2,3] │ [100,500,700] │
└────────────┴────────────┴───────────────┘
1 rows in set. Elapsed: 0.010 sec.
alpaca :) select manager_id, securities_final, volumes_final from (select manager_id, securities, volumes from (select manager_id, groupArray(security_id) as securities, groupArray(volume) as volumes from test_2 group by manager_id) where has(securities, 2)) array join securities as securities_final, volumes as volumes_final
SELECT
manager_id,
securities_final,
volumes_final
FROM
(
SELECT
manager_id,
securities,
volumes
FROM
(
SELECT
manager_id,
groupArray(security_id) AS securities,
groupArray(volume) AS volumes
FROM test_2
GROUP BY manager_id
)
WHERE has(securities, 2)
)
ARRAY JOIN
securities AS securities_final,
volumes AS volumes_final
┌─manager_id─┬─securities_final─┬─volumes_final─┐
│ 1 │ 1 │ 100 │
│ 1 │ 2 │ 500 │
│ 1 │ 3 │ 700 │
└────────────┴──────────────────┴───────────────┘
3 rows in set. Elapsed: 0.012 sec.
-- now with dim table with hierarchy
alpaca :) create table test_2_dim_security (security_id UInt8, security_label String, security_group_id UInt8, security_group_label String) ENGINE = Log
CREATE TABLE test_2_dim_security
(
security_id UInt8,
security_label String,
security_group_id UInt8,
security_group_label String
)
ENGINE = Log
Ok.
0 rows in set. Elapsed: 0.005 sec.
alpaca :) insert into test_2_dim_security values (1, 'Apple', 1, 'Tech'), (2, 'Nike', 2, 'Shoes'), (3, 'Amazon', 1, 'Tech'), (4, 'Netflix', 1, 'Tech')
INSERT INTO test_2_dim_security VALUES
Ok.
4 rows in set. Elapsed: 0.006 sec.
alpaca :) select manager_id, securities_final, volumes_final from (select manager_id, securities, volumes from (select manager_id, groupArray(security_id) as securities, groupArray(volume) as volumes from test_2 group by manager_id) where hasAny(securities, (select groupArray(security_id) from test_2_dim_security where security_group_id = 2) )) array join securities as securities_final, volumes as volumes_final
SELECT
manager_id,
securities_final,
volumes_final
FROM
(
SELECT
manager_id,
securities,
volumes
FROM
(
SELECT
manager_id,
groupArray(security_id) AS securities,
groupArray(volume) AS volumes
FROM test_2
GROUP BY manager_id
)
WHERE hasAny(securities,
(
SELECT groupArray(security_id)
FROM test_2_dim_security
WHERE security_group_id = 2
))
)
ARRAY JOIN
securities AS securities_final,
volumes AS volumes_final
┌─manager_id─┬─securities_final─┬─volumes_final─┐
│ 1 │ 1 │ 100 │
│ 1 │ 2 │ 500 │
│ 1 │ 3 │ 700 │
└────────────┴──────────────────┴───────────────┘
3 rows in set. Elapsed: 0.019 sec.
alpaca :) select groupArray(security_id) from test_2_dim_security where security_group_id = 2
SELECT groupArray(security_id)
FROM test_2_dim_security
WHERE security_group_id = 2
┌─groupArray(security_id)─┐
│ [2] │
└─────────────────────────┘
1 rows in set. Elapsed: 0.006 sec.
-- now with hierarchy and multiple conditions on multiple levels, cutting on one level.
alpaca :) insert into test_2 values (3, 2, 1500)
INSERT INTO test_2 VALUES
Ok.
1 rows in set. Elapsed: 0.004 sec.
alpaca :) select manager_id, securities_final, volumes_final from (select manager_id, securities, volumes from (select manager_id, groupArray(security_id) as securities, groupArray(volume) as volumes from test_2 group by manager_id) where hasAny(securities, (select groupArray(security_id) from test_2_dim_security where security_group_id = 2) ) and not hasAny(securities, [1])) array join securities as securities_final, volumes as volumes_final
SELECT
manager_id,
securities_final,
volumes_final
FROM
(
SELECT
manager_id,
securities,
volumes
FROM
(
SELECT
manager_id,
groupArray(security_id) AS securities,
groupArray(volume) AS volumes
FROM test_2
GROUP BY manager_id
)
WHERE hasAny(securities,
(
SELECT groupArray(security_id)
FROM test_2_dim_security
WHERE security_group_id = 2
)) AND NOT hasAny(securities, [1])
)
ARRAY JOIN
securities AS securities_final,
volumes AS volumes_final
┌─manager_id─┬─securities_final─┬─volumes_final─┐
│ 3 │ 3 │ 1300 │
│ 3 │ 2 │ 1500 │
└────────────┴──────────────────┴───────────────┘
2 rows in set. Elapsed: 0.023 sec.