ORACLE 10g,如何将不同ID号的数据按ID顺序合并成一行 20
数据举例:表A:codeIDcontext011hh012dd013c,c021ww022e,e023tt查询出的结果应为:codeIDcontext011hhddc,c...
数据举例:
表A:
code ID context
01 1 hh
01 2 dd
01 3 c,c
02 1 ww
02 2 e,e
02 3 tt
查询出的结果应为:
code ID context
01 1 hhddc,c
02 1 wwe,ett
既按code分组,再按ID顺序将context合并为一行,在ORACLE 10g环境,我试了wm_concat函数,select wm_concat(context) from A group by code;发现不能按ID顺序合并,请高手指教! 展开
表A:
code ID context
01 1 hh
01 2 dd
01 3 c,c
02 1 ww
02 2 e,e
02 3 tt
查询出的结果应为:
code ID context
01 1 hhddc,c
02 1 wwe,ett
既按code分组,再按ID顺序将context合并为一行,在ORACLE 10g环境,我试了wm_concat函数,select wm_concat(context) from A group by code;发现不能按ID顺序合并,请高手指教! 展开
1个回答
展开全部
加上order by 试试
select wm_concat(context) from A group by code
order by code,id;
或者
select wm_concat(context) from A group by code
order by id;
select wm_concat(context) from A group by code
order by code,id;
或者
select wm_concat(context) from A group by code
order by id;
追问
这两条SQL均在 order by 之后报:不是group by 表达式
追答
select code,substr(max(sys_connect_by_path(context,',')),2) context from (select a.*,row_number()over(partition by code order by id) rn from a) group by code
start with rn=1
connect by rn-1=prior rn and code=prior code
order by code;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询