I have this sample database:
CREATE TABLE t_arrays (
id integer not null,
string_array ARRAY,
number_array ARRAY,
date_array ARRAY,
CONSTRAINT pk_t_arrays PRIMARY KEY (ID)
);
INSERT INTO t_arrays VALUES (1, null, null, null);
INSERT INTO t_arrays VALUES (2, (), (), ());
INSERT INTO t_arrays VALUES (3, ('a'), (1), ('1981-07-10'));
INSERT INTO t_arrays VALUES (4, ('a', 'b'), (1, 2), ('1981-07-10', '2000-01-01'));
Now I would like to use the TABLE function as documented on this section of the manual:
http://www.h2database.com/html/functions.html#tableThe goal is to self-join the t_arrays table in order to unnest the string_array for instance. The desired output would then be:
+----+--------------+
| ID | COLUMN_VALUE |
+----+--------------+
| 2 | a |
| 3 | a |
| 3 | b |
+----+--------------+
Intuitively, I'd expect this to work, but it fails. The self-join on id =
t1.id is not recognised:
select
t1.id, t2.column_value
from t_arrays t1
join table(column_value varchar = (select string_array from t_arrays where id =
t1.id)) t2
This works, so my understanding of syntax seems to be correct:
select
t1.id, t2.column_value
from t_arrays t1
join table(column_value varchar = (select string_array from t_arrays where id = 4)) t2
Is there a way to do this with H2? I think being able to reference tables / columns from the outer scope of the TABLE function is important, not only when joining new nested selects, but also when unnesting arrays from stored functions, e.g. something like this:select
t1.id, t2.column_value
from t_arrays t1
join table(column_value varchar = my_array_function(
t1.id)) t2