I have a query which works fine with PostgreSQL, and I need to use it on H2Database.
Important for the tables is basically only an id integer field.
The example-query on PostgreSQL with the result is here:
select id,
array_to_string(
array(select id from table1)
,',')
from table2
order by id
Result:
id | array_to_string
2 | 1,3,4,5,2
3 | 1,3,4,5,2
4 | 1,3,4,5,2
6 | 1,3,4,5,2
7 | 1,3,4,5,2
8 | 1,3,4,5,2
9 | 1,3,4,5,2
10 | 1,3,4,5,2
For H2, I implemented the user-defined functions array_to_string and array as follows:
public class H2Functions {
public static String arrayToString(final Object[] array, final String separator) {
return StringUtils.join(array, separator);
}
public static Object array(final Object row) {
return "???";
}
}
The problem is that I can not implement array as I don't know what it get's passed.
The query fails with:
Scalar subquery contains more than one row;
How can I convince H2 to return something which array can work with ?
table1for each row oftable2seems very strange to me. Are you sure you do not want a co-related sub-query? Or even a proper join andgroup bybetween those tables?