You would be better off by writing a common function which returns you the output string after passing the sql query as an input string.
Here is a query to get the results but if you are expecting a large number of rows, might get longer. This one works for 5 rows.
Code:
select max(decode(rnbr, 1, emp_no, null)) || ',' ||
max(decode(rnbr, 2, emp_no, null)) || ',' ||
max(decode(rnbr, 3, emp_no, null)) || ',' ||
max(decode(rnbr, 4, emp_no, null)) || ',' ||
max(decode(rnbr, 5, emp_no, null)) emp_string
from ( select dept_no, emp_no, row_number() over (partition by dept_no order by dept_no) as rnbr
from emp )
group by dept_no
/