search
HomeDatabaseOracleHow to convert Oracle horizontal table to vertical table

In Oracle, you can use the pivot() function to convert a horizontal table into a vertical table. This function is used to convert rows into columns. The syntax is "SELECT * FROM (data query set) PIVOT (SUM (row The value of the column after the conversion)FOR needs to be converted into the column IN (the value of the column after the conversion))".

How to convert Oracle horizontal table to vertical table

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

How to convert Oracle horizontal table to vertical table

Oracle row to column conversion is to use the value of a certain field as a unique value, and then convert the row value of another field into its column value. Here we still use the student score table of our system as an example. The score table records (rows) correspond to the scores of each student in each subject. Then we need to make a report that displays all the subjects of each student as one column. performance information. The case data is as follows:

How to convert Oracle horizontal table to vertical table

#So how do we achieve it? Here are several methods one by one:

1. First of all, what we must think of is that it should be possible to use Oracle grouping (group by). The implementation code is as follows:

select c.stuname,
       --利用分组聚合函数
       sum(decode(b.coursename, '英语(2018上学期)', t.score, 0)) as "英语(2018上学期)",
       sum(decode(b.coursename, '数学(2018上学期)', t.score, 0)) as "英语(2018上学期)",
       sum(decode(b.coursename, '语文(2018上学期)', t.score, 0)) as "英语(2018上学期)"
  from STUDENT.SCORE t, student.course b, student.stuinfo c
 where t.courseid = b.courseid
   and t.stuid = c.stuid
 group by c.stuname

We use group by to Students are grouped, and then use decode to convert the grade value of the corresponding course, and then sum it up to get the result value of the grade. The result is as follows:

How to convert Oracle horizontal table to vertical table

2 , Oracle11g later provides the built-in function PIVOT, which can perfectly solve this row to column requirement. The specific syntax structure is as follows:

SELECT * FROM (数据查询集)
PIVOT 
(
 SUM(Score/*行转列后 列的值*/) FOR 
 coursename/*需要行转列的列*/ IN (转换后列的值)
)

The specific code is as follows:

select * from  (select c.stuname,
       b.coursename,
       t.score
  from STUDENT.SCORE t, student.course b, student.stuinfo c
 where t.courseid = b.courseid
   and t.stuid = c.stuid )  /*数据源*/
PIVOT 
(
 SUM(score/*行转列后 列的值*/) 
 FOR coursename/*需要行转列的列*/ IN ('英语(2018上学期)' as 英语,'数学(2018上学期)' as 数学,'语文(2018上学期)' as 语文 )
) ;

The result is as follows:

How to convert Oracle horizontal table to vertical table

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to convert Oracle horizontal table to vertical table. 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
Using Oracle Software: Database Management and BeyondUsing Oracle Software: Database Management and BeyondApr 24, 2025 am 12:18 AM

In addition to database management, Oracle software is also used in JavaEE applications, data grids and high-performance computing. 1. OracleWebLogicServer is used to deploy and manage JavaEE applications. 2. OracleCoherence provides high-performance data storage and caching services. 3. OracleExadata is used for high performance computing. These tools allow Oracle to play a more diversified role in the enterprise IT architecture.

Oracle's Role in the Business WorldOracle's Role in the Business WorldApr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Oracle Software in Action: Real-World ExamplesOracle Software in Action: Real-World ExamplesApr 22, 2025 am 12:12 AM

Oracle software applications in the real world include e-commerce platforms and manufacturing. 1) On e-commerce platforms, OracleDatabase is used to store and query user information. 2) In manufacturing, OracleE-BusinessSuite is used to optimize inventory and production planning.

Oracle Software: Applications and IndustriesOracle Software: Applications and IndustriesApr 21, 2025 am 12:01 AM

The reason why Oracle software shines in multiple fields is its powerful application and customized solutions. 1) Oracle provides comprehensive solutions from database management to ERP, CRM, SCM, 2) its solutions can be customized according to industry characteristics such as finance, medical care, manufacturing, etc. 3) Successful cases include Citibank, Mayo Clinic and Toyota, 4) The advantages lie in comprehensiveness, customization and scalability, but challenges include complexity, cost and integration issues.

Choosing Between MySQL and Oracle: A Decision GuideChoosing Between MySQL and Oracle: A Decision GuideApr 20, 2025 am 12:02 AM

Choosing MySQL or Oracle depends on project requirements: 1. MySQL is suitable for small and medium-sized applications and Internet projects because of its open source, free and ease of use; 2. Oracle is suitable for core business systems of large enterprises because of its powerful, stable and advanced functions, but at a high cost.

Oracle's Products: A Deep DiveOracle's Products: A Deep DiveApr 19, 2025 am 12:14 AM

Oracle's product ecosystem includes databases, middleware and cloud services. 1. OracleDatabase is its core product, supporting efficient data storage and management. 2. Middleware such as OracleWebLogicServer connects to different systems. 3. OracleCloud provides a complete set of cloud computing solutions.

MySQL and Oracle: Key Differences in Features and FunctionalityMySQL and Oracle: Key Differences in Features and FunctionalityApr 18, 2025 am 12:15 AM

MySQL and Oracle each have advantages in performance, scalability, and security. 1) Performance: MySQL is suitable for read operations and high concurrency, and Oracle is good at complex queries and big data processing. 2) Scalability: MySQL extends through master-slave replication and sharding, and Oracle uses RAC to provide high availability and load balancing. 3) Security: MySQL provides fine-grained permission control, while Oracle has more comprehensive security functions and automation tools.

Oracle: The Powerhouse of Database ManagementOracle: The Powerhouse of Database ManagementApr 17, 2025 am 12:14 AM

Oracle is called the "Powerhouse" of database management because of its high performance, reliability and security. 1. Oracle is a relational database management system that supports multiple operating systems. 2. It provides a powerful data management platform with scalability, security and high availability. 3. Oracle's working principles include data storage, query processing and transaction management, and supports performance optimization technologies such as indexing, partitioning and caching. 4. Examples of usage include creating tables, inserting data, and writing stored procedures. 5. Performance optimization strategies include index optimization, partition table, cache management and query optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!