Recently there have been group by the encoding process, some columns have multiple values, and I need to conduct these columns stitching multiple values, and share with you.
The following table student:
We hope that class into groups of information tiling effect is as follows
Group first thought is certainly group by:
select * from student s group by s.class;
We will find that this will complain because the name and age have multiple values, this time we can use wm_concat () method to solve.
select wm_concat(s.name),wm_concat(s.age),s.class from student s gruop by s.class;
But the wording and the emergence of a new problem, you will find the name and age of the results of this column is
Simply put wm_concat preceded by a to_char just fine, and the result is above the desired result.
TO_CHAR SELECT (wm_concat (s.name)) AS name,
TO_CHAR (wm_concat (s.age)) AS Age,
s.class
from Student S
Gruop by s.class;
------------- --------
there LISTAGG a function ( 'parameters a', 'two parameters'), the function and effect similar wm_comcat, the first parameter is a field, the second parameter is a plurality of values between the separator, but the usage is somewhat different, examples are as follows:
select listagg(s.name,'-')within group(order by class) as name,
to_char(wm_concat(s.age)),
s.class
from student s
gruop by s.class;
---------------------
Source: https://blog.csdn.net/yufeng1397/article/details/78672607