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

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

青灯夜游
青灯夜游Original
2022-03-17 15:04:365168browse

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 (列名列表))

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 (英语,数学,语文))

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!

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