I do not think this is relevant with subquery. But I kind of don't know well how to search this issue.
I would like to concatenate two query results, one is from a table, another is from b table. I think this is simple query, but I kind of forgot it.
select id, value as a_value from a where id = 1 and sub_id = 1;
select id, value as b_value from b where id = 1 and sub_id = 2;
Ultimately, I would like to obtain a_value || b_value (aggregation of two values) , how could I achieve this?
The way I thought was
select aa.a_value || bb.b_value (
(select id, value from a where id = 1 and sub_id = 1) aa,
(select id, value from a where id = 1 and sub_id = 2) bb
)
It seems definitely wrong. Could you anyone help this out?
The point of my question was I want to some reference for each query, in the above query, aa and bb were references I wanted to gain. Actually, the real query I want to achieve is a little complicate than above.
select aa.a_value || bb.b_value (
(select id, to_clob(value) as a_value from a where id = 1 and sub_id = 1) aa,
(select id, listagg(value, '') as b_value within group (order by sub_id) from a where id = 1 and sub_id >=2 group by id) bb);
The reason why I want to do this is that after aggregation, I run into 'ORA-01489: result of string concatenation is too long'. One of the solution can be query above. So, I would like to do that query.
Oracle version info is Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
This is the example a table has following records.
id sub_id value
1 1 a .... a (1000 char)
1 2 b .... b (1000 char)
1 3 c .... c (1000 char)
1 4 d .... d (1000 char)
1 5 e .... e (1000 char)
The string I want to create is a .... ab .... bc .... cd .... de .... e, aggregation of whole strings belonging to id = 1. In order to do it, I can use listagg but listadd function's return type is varchar2. After aggregation, the returned string becomes 5,000 chars so it returns error 'ORA-01489: result of string concatenation is too long' because varchar2 is limited to 4,000 chars. In order to solve this, I had to convert the first string 'a .... a' into clob type using TO_CLOB function and then I aggregate the rest of the strings ('b .... b', 'c .... c', 'd .... d', 'e .... e') into that clob, a .... a. In order to do it, I would like to create query for it.