-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.sql
209 lines (179 loc) · 9.84 KB
/
demo.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
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
-- .mode line
.mode box
create table d (
n text not null,
i integer not null,
v integer not null,
primary key ( n, i ) );
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Current table contents'
.print ''
.print '```'
insert into d ( n, i, v ) values
( 'a', 1, 11 ),
( 'a', 2, 21 ),
( 'a', 3, 31 ),
( 'b', 1, 12 ),
( 'b', 2, 22 ),
( 'c', 1, 13 ),
( 'c', 2, 23 ),
( 'c', 3, 33 ),
( 'c', 4, 43 );
select * from d order by n, i;
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Several ways to aggregate values with `*group*()` functions, all of them working as expected:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w as "json_group_array() nested"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
select distinct
n as n,
json_group_object( i, v ) over w as "json_group_object()"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
select distinct
n as n,
json_group_array( v ) over w as "json_group_array() flat"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
select distinct
n as n,
group_concat( '(' || i || ',' || v || ')', ', ' ) over w as "group_concat()"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Using several aggregate functions in the same table using the same window definition all fail, with the'
.print 'exception of the *last* aggregate and `group_concat()`, which do work as expected:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w as "json_group_array() nested",
json_group_object( i, v ) over w as "json_group_object()",
json_group_array( v ) over w as "json_group_array() flat",
group_concat( '(' || i || ',' || v || ')', ', ' ) over w as "group_concat()"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w as "json_group_array() nested",
json_group_object( i, v ) over w as "json_group_object()",
group_concat( '(' || i || ',' || v || ')', ', ' ) over w as "group_concat()",
json_group_array( v ) over w as "json_group_array() flat"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w as "json_group_array() nested",
group_concat( '(' || i || ',' || v || ')', ', ' ) over w as "group_concat()",
json_group_object( i, v ) over w as "json_group_object()",
json_group_array( v ) over w as "json_group_array() flat"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w as "json_group_array() nested",
group_concat( '(' || i || ',' || v || ')', ', ' ) over w as "group_concat()",
json_group_array( v ) over w as "json_group_array() flat",
json_group_object( i, v ) over w as "json_group_object()"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print '`group_concat()` works even with unquoted values, even in the midst of other aggregators:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w as "json_group_array() nested",
group_concat( '(' || i || ',' || v || ')', ', ' ) over w as "group_concat()",
group_concat( i ) over w as "group_concat( i )",
group_concat( v ) over w as "group_concat( v )",
json_group_array( v ) over w as "json_group_array() flat",
json_group_object( i, v ) over w as "json_group_object()"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Using `json_group_array()` fails where not last in a series of aggregations:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( i ) over w as "json_group_array( i ) 1",
json_group_array( i ) over w as "json_group_array( i ) 2",
json_group_array( i ) over w as "json_group_array( i ) 3"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Turning values into strings does not affect outcomes:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( '(' || i || ')' ) over w as "json_group_array( i ) 1",
json_group_array( '(' || i || ')' ) over w as "json_group_array( i ) 2",
json_group_array( '(' || i || ')' ) over w as "json_group_array( i ) 3"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Using a separate but identical window definition for each aggregate does not affect outcomes:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( json_array( i, v ) ) over w1 as "json_group_array() nested",
group_concat( '(' || i || ',' || v || ')', ', ' ) over w2 as "group_concat()",
json_group_array( v ) over w3 as "json_group_array() flat",
json_group_object( i, v ) over w4 as "json_group_object()"
from d
window w1 as ( partition by n order by i range between unbounded preceding and unbounded following ),
w2 as ( partition by n order by i range between unbounded preceding and unbounded following ),
w3 as ( partition by n order by i range between unbounded preceding and unbounded following ),
w4 as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print 'Aggregate function `sum()` is not affected; results show unambiguously that summing is indeed done over the'
.print 'values shown in the `group_concat()` lists, even in the midst of other aggregators:'
.print ''
.print '```'
select distinct
n as n,
group_concat( i ) over w as "group_concat( i )",
group_concat( v ) over w as "group_concat( v )",
sum( i ) over w as "sum( i )",
sum( v ) over w as "sum( v )",
json_group_array( i ) over w as "json_group_array( i )"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'
.print ''
.print '------------------------------------------------------------------------------------------------------------'
.print '`json_group_array()` also fails when not coming last when combined with `sum()`; observe the discrepancies'
.print 'between numbers listed and numbers summed:'
.print ''
.print '```'
select distinct
n as n,
json_group_array( i ) over w as "json_group_array( i )",
sum( i ) over w as "sum( i )",
json_group_array( v ) over w as "json_group_array( v )",
sum( v ) over w as "sum( v )"
from d
window w as ( partition by n order by i range between unbounded preceding and unbounded following );
.print '```'