Get the App
SLTechnology News&Howtos  ›  Database  › 

PIVOT: row-to-column function

Shulou Source: shulou.com Published: 2022-06-01 13:47:39 10月02日 Update

PIVOT function, row to column

The PIVOT function has the following format:

PIVOT(([Aggregate Column Value]) FOR [Column Name Before Row Conversion] IN([Column Name After Row Conversion 1],[Column Name After Row Conversion 2],[Column Name After Row Conversion 3],... [column name after row transition N]))

That is, we use SUM,COUNT,AVG and other Sql aggregation functions, that is, the aggregation method of calculating columns after row conversion.

[Aggregate column values] Column names to aggregate

[Column name before row to column] This is the column name that needs to be converted from row to column.

[Column name after row conversion] Here, you need to declare that the value of the row will be converted to the column name after column conversion, because the column name after conversion is actually the value before conversion, so in the above format,[Column name after row conversion 1],[Column name after row conversion 2],[Column name after row conversion 3],...... [The column name N after row conversion] is actually the value of each row before row conversion.

SELECT FROM [StudentScores] /Data Sources/

AS P

PIVOT

(

SUM(Score/Score----Value of column after row transfer/) FOR

p.Subject/Course----Column/ IN (Value of [Language],[Math],[English],[Biology]/Column */)

) AS T

SqlServer PIVOT function quickly realize row to column, UNPIVOT realize column to column- PowerCoder -Blog Garden http://www.cnblogs.com/OpenCoder/p/6668882.html

Tags: Functions that is formats lines that is scores blogs math data sources data sources methods biology Chinese courses English Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Linux MySQL MariaDB Shulou Technology