How to achieve row-column conversion in oracle
Methods to implement row-column conversion: 1. Use the PIVOT() function to implement row-to-column conversion, with the syntax "SELECT * FROM (data set) PIVOT (SUM(Score) FOR coursename IN (converted column value))" "; 2. Use the unpivot() function to realize column switching.
The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.
Oracle row to column
Oracle row to column is to use the value of a certain field as the only value, and then use the row value of another field Convert to 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:
#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:
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:
Oracle column to row conversion
Oracle column to row conversion is to convert the fields of the columns in a row according to the unique value of the row Convert to multiple rows of data. For example, the basic data of the student performance table in the Oracle row to column conversion above is that one student's subject performance corresponds to one record. Then we converted the rows into columns and turned it into a record corresponding to the scores of a student in each column (math, English, Chinese). So this article is to convert the previously converted student score table (backup table score_copy) into columns and rows again and convert it into original data. The case data is as follows:
So how do we implement column switching? Two commonly used methods are introduced below:
1. Use union all for splicing, which can perfectly convert the corresponding columns into row records. The specific code is as follows:
select t.stuname, '英语' as coursename ,t.英语 as score from SCORE_COPY t union all select t.stuname, '数学' as coursename ,t.数学 as score from SCORE_COPY t union all select t.stuname, '语文' as coursename ,t.语文 as score from SCORE_COPY t
The results are as follows:
2. This problem can also be perfectly solved by using Oracle's own column-to-row function unpivot. The specific syntax structure is as follows:
select 字段 from 数据集 unpivot(自定义列名/*列的值*/ for 自定义列名 in(列名))
The implementation code is as follows:
select stuname, coursename ,score from score_copy t unpivot (score for coursename in (英语,数学,语文))
The results are as follows:
Recommended tutorial: "Oracle Tutorial"
The above is the detailed content of How to achieve row-column conversion in oracle. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

Data import method: 1. Use the SQLLoader utility: prepare data files, create control files, and run SQLLoader; 2. Use the IMP/EXP tool: export data, import data. Tip: 1. Recommended SQL*Loader for big data sets; 2. The target table should exist and the column definition matches; 3. After importing, data integrity needs to be verified.

There are the following methods to get time in Oracle: CURRENT_TIMESTAMP: Returns the current system time, accurate to seconds. SYSTIMESTAMP: More accurate than CURRENT_TIMESTAMP, to nanoseconds. SYSDATE: Returns the current system date, excluding the time part. TO_CHAR(SYSDATE, 'YYY-MM-DD HH24:MI:SS'): Converts the current system date and time to a specific format. EXTRACT: Extracts a specific part from a time value, such as a year, month, or hour.

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.
