oracle中,使用wm_concat(column)函數,可以進列欄位值合并,合并的欄位預設通過逗號分隔,也可自行指定。如:
wm_concat(col1);
replace(wm_concat(col1),',','|'); --自訂分隔號,使用replace將逗號進行替換
(wm_concat(col1||'|')並不是改變分隔號,結果為:col1|,col1|,.. 非col1|col2..)
wm_concat(col1||'('||col2||'kg)'); --多個欄位組合為一個串,結果為col1(col2kg),col1(col2kg)... 分隔號不替換,預設為逗號
表table1內容形式:
no_id |
name |
weight |
22 |
apple |
12 |
22 |
yellow |
43 |
33 |
red |
32 |
33 |
fox |
11 |
select no_id,wm_concat(name) from table1 group by no_id order by no_id;
#results: 22---apple,yellow 33---red,fox
select no_id,wm_concat(name||'('||weight||'kg)') from table1 group by no_id;
#results: 22---apple(12kg),yellow(43kg) 33---red(32kg),fox(11kg)
--實際例子
SELECT NO,role_remark,name,wm_concat(member_name) FROM (
SELECT dense_rank() OVER(PARTITION BY c.name ORDER BY c.name,b.role_remark) AS NO, b.role_remark,a.group_id,a.member_id,a.member_name,SUBSTR(a.group_id,-3,3),c.name
from T_BPM_GROUP_USER a,T_WFD_ROLE b,cqauth.company c
WHERE b.id=SUBSTR(a.group_id,0,4) AND b.FLOWTYPE_NAME like '網路割接%' AND b.role_remark LIKE '%基站開通%'
AND SUBSTR(a.group_id,-3,3)=c.companyid
) GROUP BY NO,role_remark,name;
註:group by 需要包含select中的所有欄位,除了計算或聯結的欄位外,如count(*)等
對於製造分組,可使用row_number()over 或 rank()over 或 dense_rank()over等分析函數,如上面的實際例子。分組後再按序號進行相關欄位的合并
本文出自 “奔跑的羚羊” 部落格,請務必保留此出處http://heshw.blog.51cto.com/5891747/1294263