Home >Database >Oracle >How to convert multiple rows to one in Oracle

How to convert multiple rows to one in Oracle

WBOY
WBOYOriginal
2022-03-07 15:12:4622453browse

In Oracle, you can use the listagg function with the "order by" clause to convert multiple rows into one row. This statement can sort the data and then splice the sorted results together. The syntax is "listagg (column name) ,'separator') within group(order by column name)".

How to convert multiple rows to one in Oracle

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

How to convert multiple rows into one row in oracle

Due to the need, the sub-organizations under the obtained organization need to be combined into one row of data, and Oracle’s own function is used

listagg (column name, 'separator') within group (order by column name)

That is, within each group, LISTAGG sorts the columns according to the order by clause, and sorts The final results are spliced ​​together

My organizational structure is a tree structure, and the following SQL queries the names of all sub-departments under the current department.

SELECT LISTAGG(O.ORGNAME,',') WITHIN GROUP(ORDER BY LEVEL)
FROM ORGANIZATION O
START WITH O.ORGID = 1000 CONNECT BY PRIOR O.ORGID = O.PID AND LEVEL<4

Note: The above SQL uses the Oracle keyword LEVEL, which indicates which level the data in the tree structure is at
The above SQL also uses the Oracle tree query statement START WITH … CONNECT BY PRIOR …
If the query is for all parent nodes of the node, the above START WITH SQL should be changed to:
START WITH O.ORGID = 1000 CONNECT BY PRIOR O.PID = O. ORGID (The fields after PRIOR are not the same as the previous order)

ORGANIZATION table data is as follows

##1000Primary school01100First grade10001200Second grade10001101101 class1100##1102110312011.LEVEL
ORGID ORGNAME PID
102 class 1100
103 class 1100
201Class 1200
SELECT ORGNAME,LEVEL FROM ORGANIZATION 
START WITH ORGID = 1000 CONNECT BY PRIOR O.ORGID = O.PID

The execution results are as follows

ORGNAMEPrimary school一Grade##Second grade2101 class3 Class 1023Class 1033201Class32.LISTAGG…WITHIN GROUP…
SELECT LISTAGG(O.ORGNAME,',') WITHIN GROUP(ORDER BY LEVEL)
FROM ORGANIZATION O
LEVEL
1
2
The execution results are as follows:

Elementary School , first grade, second grade, Class 101, Class 102, Class 103, Class 201

3. Put all the child nodes under the parent node into one line

SELECT LISTAGG(O.ORGNAME,',') WITHIN GROUP(ORDER BY LEVEL)
FROM ORGANIZATION O
START WITH O.ORGID = 1000 CONNECT BY PRIOR O.ORGID = O.PID AND LEVEL<4
The execution results are as follows:

Elementary school, first grade, second grade, Class 101, Class 102, Class 103, Class 201

Recommended tutorial: "
Oracle Video Tutorial

"

The above is the detailed content of How to convert multiple rows to one in Oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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