所以這是我table--如何在oracle中使用sys_connect_by_path函數時省略第一個和最後一個逗號?
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連接字符串值。 我的代碼is--我的代碼
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個湯姆物理學,計算機,ED 90,95,75
我試圖修正功能,但我不能得到成功。 如果我的數據是固定的,我可以通過路徑來連接sys連接,但是這裏的數據不是固定的。 所以請幫助..
非常感謝Rob。 – 2012-03-06 14:00:04