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 use Unpivot function to realize column Wrap in SQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article shows you how to use the Unpivot function in SQL to achieve column switching, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Create data

CREATE TABLE dbo.CustomerPhones (CustomerID INT PRIMARY KEY,-- FK Phone1 VARCHAR (32), Phone2 VARCHAR (32), Phone3 VARCHAR (32); INSERT dbo.CustomerPhones (CustomerID, Phone1, Phone2, Phone3) VALUES (1 mini 705-491-1111, '705-491-1110, NULL), (2) NULL, NULL), (3) Mint 416-493-33,' 416-493-3330, '416-493-3339'); select * from dbo.CustomerPhones

View the data as follows:

DataDataUpivot implements row-to-column conversion

Select * from dbo.CustomerPhones-data source unpivot (Phone FOR Phones IN (Phone1, Phone2, Phone3) # # the results of columns such as Phone1, Phone2, Phone3 are converted to the results on the row, and become a column, # # and there is a new column named Phone) up

The results are as follows:

ResultResult

The above is how to use the Unpivot function in SQL to achieve column switching. 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: 0

*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