Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize column-column interchange operation in SQLServer

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 205

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report