so this is my table–
create table student
(
stu_id int,
s_name nvarchar(max),
s_subject nvarchar(max),
marks varchar(20)
)
and the values are
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');
so what i have done is extracted data which occurred thrice. and then concatenated the string values using sys_connect_by_path.
My code 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
output of my code is —
stu_id s_name s_subject marks
125 Tom ,Physics,Computer,ED, ,90,95,75,
the code is working perfectly, but I am using comma as seprator, and i just want to get rid of comma at the start and at the end. in s_subject column.
what I wants is
stu_id s_name s_subject marks
125 Tom Physics,Computer,ED 90,95,75
I tried trim function, but i could not get success.
I can substr the sys connect by path if my data is fixed, but here data is not fixed.
So pls help..
Regards,
Rob.
PS: Thanks and +1 for providing the create table statement and insert statements.