分组合并多列

WBOY
Release: 2016-06-07 14:56:31
Original
1248 people have browsed it

Oracle9i及以前的版本中,没有可直接支持的分组后,用某个特殊字符进行分隔列 像10g有wm_concat,11g可以用listagg(字段,分隔字符) within group(order by 字段) Oracle SELECT 分组列,ltrim(MAX(sys_connect_by_path(字符串列,',')),',')FROM (SELECT 分组列

Oracle9i及以前的版本中,没有可直接支持的分组后,用某个特殊字符进行分隔列
像10g有wm_concat,11g可以用listagg(字段,分隔字符) within group(order by 字段)
Oracle
SELECT 分组列,ltrim(MAX(sys_connect_by_path(字符串列,',')),',') FROM ( SELECT 分组列,字符串列,rank()over(ORDER BY 分组列) + row_number()over(order by 分组列) RN,row_number()over(partition by 分组列 order by 分组列) RM FROM 表) start with RM=1 connect by prior RN=RN-1 group by 分组列 ; --在FROM 表这个地方你可以过滤掉这些空值 --或在ltrim(MAX(sys_connect_by_path(字符串列,',')),',')这个地方 --再加个这个就可以rtrim(ltrim(MAX(sys_connect_by_path(字符串列,',')),','),',')
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn