在oracle中使用sys_connect_by_path函数时如何省略第一个和最后一个逗号?

时间:2012-03-06 12:50:50

标签: oracle sqlplus

所以这是我的表 -

create table student
(
stu_id int,
s_name nvarchar(max),
s_subject nvarchar(max),
marks varchar(20)
)

,值为

insert into student values(123,'pammy','English','88');
insert into student values(123,'pammy','Maths','56');
insert into student values(124,'watts','Biology','98');
insert into student values(125,'Tom','Physics','90');
insert into student values(125,'Tom','Computer','95');
insert into student values(125,'Tom','ED','75');

所以我所做的是提取三次的数据。然后使用sys_connect_by_path连接字符串值。 我的代码是 -

 select stu_id,s_name,
max(sys_connect_by_path(s_subject, ', ' )) s_subject,
max(sys_connect_by_path(marks, ', ' )) marks

    from (select stu_id,s_name,s_subject,marks, 
                    row_number() over 
                   (partition by stu_id order by s_subject) rn
             from student
               )
   start with rn = 1
    connect by prior rn = rn-1 and prior stu_id = stu_id
    group by stu_id,s_name
having stu_id in ( select stu_id 
                    from student
                    group by stu_id 
                    having count(stu_id) >3 )
  order by stu_id,s_name

我的代码输出是 -

stu_id      s_name  s_subject              marks
125         Tom     ,Physics,Computer,ED,    ,90,95,75,

代码工作正常,但我使用逗号作为seprator,我只是想在开头和结尾摆脱逗号。在s_subject列中。

我想要的是

stu_id s_name s_subject标记 125 Tom Physics,Computer,ED 90,95,75

我尝试了修剪功能,但我无法获得成功。 如果我的数据是固定的,我可以通过路径继承sys连接,但这里的数据不是固定的。 所以请帮助..

3 个答案:

答案 0 :(得分:2)

以下是我在Oracle 11.2中的表现:

SQL> select stu_id, s_name,
  2      listagg(s_subject, ', ' ) within group (order by s_subject) s_subject,
  3      listagg(marks, ', ' ) within group (order by s_subject) marks
  4  from student
  5  group by stu_id, s_name;

STU_ID S_NAME          S_SUBJECT                 MARKS
------ --------------- ------------------------- ---------------
   123 pammy           English, Maths            88, 56
   124 watts           Biology                   98
   125 Tom             Computer, ED, Physics     95, 75, 90

请注意,我按主题订购了两个列表,因此订单在每个列表列中对应。

答案 1 :(得分:1)

SQL>  select stu_id
  2        , s_name
  3        , ltrim(max(sys_connect_by_path(s_subject, ', ' )),', ') s_subject
  4        , ltrim(max(sys_connect_by_path(marks, ', ' )),', ') marks
  5     from ( select stu_id
  6                 , s_name
  7                 , s_subject
  8                 , marks
  9                 , row_number() over (partition by stu_id order by s_subject) rn
 10              from student
 11          )
 12    where level >= 3
 13    start with rn = 1
 14  connect by prior rn = rn - 1
 15      and prior stu_id = stu_id
 16    group by stu_id
 17        , s_name
 18  /

    STU_ID S_NAME     S_SUBJECT                      MARKS
---------- ---------- ------------------------------ --------------------
       125 Tom        Computer, ED, Physics          95, 75, 90

1 row selected.

的问候,
罗布。

PS:感谢和+1提供create table语句和insert语句。

答案 2 :(得分:0)

substr( max(sys_connect_by_path(s_subject, ', ' )),
    2, 
    length(max(sys_connect_by_path(s_subject, ', ' ))-1 )