Home > Database > Oracle > What is the SQL statement for converting Oracle columns to rows?

What is the SQL statement for converting Oracle columns to rows?

青灯夜游
Release: 2022-03-17 15:05:05
Original
5303 people have browsed it

In Oracle, you can use the unpivot() function to achieve column switching. The SQL statement is "select field list from data set unpivot(custom column name/*column value*/ for custom column name in (List of names))".

What is the SQL statement for converting Oracle columns to rows?

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

In actual business development environments, we often encounter the need to convert columns to rows in the queried data set. So how does Oracle achieve it?

Oracle column to row conversion

Oracle column to row conversion is to convert the fields of the columns in a row into multiple rows of data according to the unique value of the row.

How to achieve column switching? You can use Oracle's own column-to-row function unpivot to perfectly solve this problem

The specific syntax structure is as follows:

select 字段列表 from 数据集
unpivot(自定义列名/*列的值*/ for 自定义列名 in (列名列表))
Copy after login

Example:

What is the SQL statement for converting Oracle columns to rows?

What is the SQL statement for converting Oracle columns to rows?

The conversion code code is as follows:

select stuname, coursename ,score from
score_copy  t
unpivot
(score for coursename in (英语,数学,语文))
Copy after login

The result is as follows:

What is the SQL statement for converting Oracle columns to rows?

Recommended tutorial: "Oracle Tutorial

The above is the detailed content of What is the SQL statement for converting Oracle columns to rows?. 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