现在的位置: 首页 > 综合 > 正文

oracle wm_concat(column)函数的使用

2013年10月06日 ⁄ 综合 ⁄ 共 698字 ⁄ 字号 评论关闭

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

表中数据如下:

想要的结果为:

有两种实现方法

第一种:使用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

结果为:

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

修改为如下语句:

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

抱歉!评论已关闭.