Got a SQL from the table with 100,000 + records , one particular varchar2 field contains strings like '12345', '56789', '1111'.
I would like to create a table with split / chunks with appropriate comma seperate
example in the live link ::: select listagg(id, ',') within group (order by id) from (select course_id as id from ad.AD_STUDENT_COURSE_DETAILS)
Example :: 100 records each record has the following ids
Now Assume based on the above result sets I should be able to make my sequence dynamic and insert group of records into comma separated values (listagg) , on my own definition
If I give 22 it should get values and distribute into 4 chunks and the remaining left based on the select query results and make the respective chunks of sequence. Here in this example the 99 records from my select query have been loaded into new table with 5 records (5 sequence number) each record carry the 22 records and the remaining.
Is it possible to achieve the same in SQL query to who the results in above format or PL/SQL block?
Appreciate your help.
To split the rows into groups with N rows in each:
- Assign a row_number() to the table (if there isn't already) sequential row numbers starting at one
- Divide this row number by N, returning the ceil of it
- Group by this expression:
with rws as (
select level rn from dual
connect by level <= 100
), grps as (
ceil ( rn / 22 ) grp
from rws r
listagg ( rn, ',' )
within group (
order by rn
group by grp;