码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-03-24 23:57:52      阅读:2478      评论:0      收藏:0      [点我收藏+]

标签:alt   rom   col   body   sql   select   within   sub   数据显示   

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;

效果如下。

技术分享图片

 

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

标签:alt   rom   col   body   sql   select   within   sub   数据显示   

原文地址:https://www.cnblogs.com/liebagefly/p/8641853.html

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!