mysql的wm_concat函数_oracle wm_concat(column)函数的使用详解

oracle数据库中,使用wm_concat(column)函数,可以进行字段合并

表中数据如下:

20aeba795af55ac523f8966dea1e1d22.png

想要的结果为:

c0ca4a534243b09b5ac2391302afaf52.png

有两种实现方法

第一种:使用decode和case when进行行转列

先不进行case when

select t.u_id,

'语文'||t.a||

'数学'||t.b||

'英语'||t.c as name

from(

select u_id,sum(decode(course,'语文',score,null)) as a,

sum(decode(course,'数学',score,null)) as b,

sum(decode(course,'英语',score,null)) as c

from t_score group by u_id

) t

结果为:

03ab7739319b5fa542b0a89dc1d17934.png

不符合要求,如果没成绩就不要显示出来

修改为如下语句:

select t.u_id,

case t.a when 1 then '' else '语文'||t.a end||

case t.b when 1 then '' else '数学'||t.b end||

case t.c when 1 then '' else '英语'||t.c end as name

from(

select u_id,max(decode(course,'语文',score,1)) as a,

max(decode(course,'数学',score,1)) as b,

max(decode(course,'英语',score,1)) as c

from t_score group by u_id

) t

第二种:使用wm_concat(column)函数实现

select u_id,

wm_concat(course||score) as name

from t_score

group by u_id


版权声明:本文为weixin_36053019原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。