Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize column-column interchange operation in SQLServer

Shulou Source: shulou.com Published: 2022-05-31 19:18:27 10月03日 Update

This article shows you how to achieve row-to-column conversion operation in SQLServer, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The syntax for pivot is:

Table_source

Pivot (value_column) pivot_column for (columnlist)

Explain a little bit:

Table_source: is the table that we are going to transform. Pivot_column: this is the name of the column to which the row is to be transferred. Value_column: is the value of the column after conversion. Columnlist is the column to be generated.

In the same example above, you can write the same result using pivot:

Select StudentName, [Chinese] Chinese, [Mathematics] Math, [English] English from (select * from @ t) t1pivot (sum (Score) for Subject in ([Chinese], [English], [Mathematics])) T2

The corresponding unpivot means that the column changes rows (the column name is the value)

The syntax for unpivot is:

Table_source

Unpivot (value_column ubpivot_column for (columnlist))

The meaning of the parameter is the same as pivot. Here we can simply turn back the one that has just been transferred, so that we can get the original watch:

Select StudentName, Subject, Score from (select * from @ t) t1pivot (sum (Score) for Subject in ([Chinese], [English], [Mathematics])) t2unpivot (Score for Subject in ([Chinese], [English], [Mathematics]) T3 the above is how to achieve row-column switching in SQLServer. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Mathematics Chinese English that is ranks content skills knowledge grammar conciseness conciseness examples functions parameters meaning articles more articles results industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Shulou Tech Info MySQL Microsoft