Home > Database > Oracle > body text

How to convert multiple rows to one in Oracle

WBOY
Release: 2022-03-07 15:12:46
Original
22361 people have browsed it

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
Copy after login
Copy after login

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
Copy after login

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
Copy after login
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
Copy after login
Copy after login
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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!