在oracle中,group by后将字符拼接,以及自定义排序

1.在oracle中,group by后将字符拼接。任务:在学生表中,有studentid和subject两个字段。要求对studentid进行group by分组,并将所选科目拼接在一起。oracle中sql语句如下。

select studentid, listagg(subject, ',') within group(order by subject)
  from student
 group by studentid;

第一幅图是未分组的数据显示,第二幅图是分组后的字符串连接之后的显示。

左为图一,右为图二。

2.任务:给查询的语句自定义排序,规定studentid为3的派第一位,为4的派第二位,剩下的按照studentid排序。

sql如下:
select * from student order by decode(studentid,3,0,4,1),studentid;

效果如下。

 

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐