Oracle many common functions are combined into one row line data

Today, doing a batch job to run, we need to put in the oracle function in multiple rows of data are combined into one line, take the opportunity to play under several commonly used methods.

A, wm_concat

wm_concat function is a commonly used function is the oracle, functions: row transfer column, the column values from the query multiple rows may be used in a comma separated splicing, it becomes a data .

Example, there is now a risk table, each risk item (risk_id) corresponds to a number of rules (risk_rule), and now I have to follow the corresponding risk entry rules out inquiry.

select 
    risk_id, wm_concat(risk_rule) 
from risk_jour 
where risk_id='50010'
group by risk_id

NOTE: wm_concat function is a function oracle, mysql no function corresponding to mysql GROUP_CONCAT () function.

Two, listagg

After oracle11g listagg function is added is to achieve a built-in function string polymerization. Row column switch may also be implemented function, the multiple rows of data are combined into a certain field.

select 
    listagg(ri.risk_name, ';') within group(order by ri.risk_name) 
    as name_list 
from t_risk ri

Instructions:

  1. LISTAGG ( 'splice required data field in the table', 'separator') WITHIN GROUP (ORDER BY 'before, the data required splicing table fields')
  2. Similarly Usage aggregate functions, through Group by statements to one field of each Group, spliced ​​together.
Published 125 original articles · won praise 116 · views 20000 +

Guess you like

Origin blog.csdn.net/shipfei_csdn/article/details/103821812