--创建科目成绩表
Create Table Subject_Score (
SID Number,
SNAME Nvarchar2(20),
SubjectName Nvarchar2(20),
SubjectScore Number(5,2)
);
--插入记录
Insert Into Subject_Score Values (1, 'junephy', '语文', '80');
Insert Into Subject_Score Values (1, 'junephy', '数学', '70');
Insert Into Subject_Score Values (1, 'junephy', '外语', '90');
Insert Into Subject_Score Values (2, 'meshellria', '语文', '60');
Insert Into Subject_Score Values (2, 'meshellria', '数学', '70');
Insert Into Subject_Score Values (2, 'meshellria', '外语', '80');
--查看结果
Select * From Subject_Score;
--wmsys.wm_concat仅支持oracle10g
Select Sname, Wmsys.Wm_Concat(Subjectscore) Scores
From (Select * From Subject_Score Order By Sname, Subjectscore)
Group By Sname;
--Sys_Connect_By_Path支持oracle9i,oracle10g
Select Sname, Ltrim(Max(Sys_Connect_By_Path(Subjectscore, ',')), ',') Scores
From (Select Sname, Subjectscore, Min(Subjectscore) Over(Partition By Sname) Min_Score,
(Row_Number() Over(Order By Sname, Subjectscore)) + (Dense_Rank() Over(Order By Sname)) Numid
From Subject_Score)
Start With Subjectscore = Min_Score
Connect By Numid - 1 = Prior Numid
Group By Sname