search
HomeDatabaseMysql TutorialSQL Server 动态行转列

一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段; 方法二:使用拼接SQL,动态列字段; 方法三:使用PIVOT关系运算符,静态列字段; 方法四:使用PIVOT关系运算符,动态列字段;

   一.本文所涉及的内容(Contents)

  本文所涉及的内容(Contents)

  背景(Contexts)

  实现代码(SQL Codes)

  方法一:使用拼接SQL,静态列字段;

  方法二:使用拼接SQL,动态列字段;

  方法三:使用PIVOT关系运算符,静态列字段;

  方法四:使用PIVOT关系运算符,,动态列字段;

  二.背景(Contexts)

  其实行转列并不是一个什么新鲜的话题了,甚至已经被大家说到烂了,网上的很多例子多多少少都有些问题,所以我希望能让大家快速的看到执行的效果,所以在动态列的基础上再把表、分组字段、行转列字段、值这四个行转列固定需要的值变成真正意义的参数化,大家只需要根据自己的环境,设置参数值,马上就能看到效果了(可以直接跳转至:“参数化动态PIVOT行转列”查看具体的脚本代码)。行转列的效果图如图1所示:

SQL Server 动态行转列 三联

  (图1:行转列效果图)

  三.实现代码(SQL Codes)

  (一) 首先我们先创建一个测试表,往里面插入测试数据,返回表记录如图2所示:

  --创建测试表

  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestRows2Columns]') AND type in (N'U'))

  DROP TABLE [dbo].[TestRows2Columns]

  GO

  CREATE TABLE [dbo].[TestRows2Columns](

  [Id] [int] IDENTITY(1,1) NOT NULL,

  [UserName] [nvarchar](50) NULL,

  [Subject] [nvarchar](50) NULL,

  [Source] [numeric](18, 0) NULL

  ) ON [PRIMARY]

  GO

  --插入测试数据

  INSERT INTO [TestRows2Columns] ([UserName],[Subject],[Source])

  SELECT N'张三',N'语文',60 UNION ALL

  SELECT N'李四',N'数学',70 UNION ALL

  SELECT N'王五',N'英语',80 UNION ALL

  SELECT N'王五',N'数学',75 UNION ALL

  SELECT N'王五',N'语文',57 UNION ALL

  SELECT N'李四',N'语文',80 UNION ALL

  SELECT N'张三',N'英语',100

  GO

  SELECT * FROM [TestRows2Columns]

wps_clip_image-8842

  (图2:样本数据)

  (二) 先以静态的方式实现行转列,效果如图3所示:

  --1:静态拼接行转列

  SELECT [UserName],

  SUM(CASE [Subject] WHEN '数学' THEN [Source] ELSE 0 END) AS '[数学]',

  SUM(CASE [Subject] WHEN '英语' THEN [Source] ELSE 0 END) AS '[英语]',

  SUM(CASE [Subject] WHEN '语文' THEN [Source] ELSE 0 END) AS '[语文]'

  FROM [TestRows2Columns]

  GROUP BY [UserName]

  GO

wps_clip_image-14456

  (图3:样本数据)

  (三) 接着以动态的方式实现行转列,这是使用拼接SQL的方式实现的,所以它适用于SQL Server 2000以上的数据库版本,执行脚本返回的结果如图2所示;

  --2:动态拼接行转列

  DECLARE @sql VARCHAR(8000)

  SET @sql = 'SELECT [UserName],'

  SELECT @sql = @sql + 'SUM(CASE [Subject] WHEN '''+[Subject]+''' THEN [Source] ELSE 0 END) AS '''+QUOTENAME([Subject])+''','

  FROM (SELECT DISTINCT [Subject] FROM [TestRows2Columns]) AS a

  SELECT @sql = LEFT(@sql,LEN(@sql)-1) + ' FROM [TestRows2Columns] GROUP BY [UserName]'

  PRINT(@sql)

  EXEC(@sql)

  GO

  (四) 在SQL Server 2005之后有了一个专门的PIVOT 和 UNPIVOT 关系运算符做行列之间的转换,下面是静态的方式实现的,实现效果如图4所示:

  --3:静态PIVOT行转列

  SELECT *

  FROM ( SELECT [UserName] ,

  [Subject] ,

  [Source]

  FROM [TestRows2Columns]

  ) p PIVOT

  ( SUM([Source]) FOR [Subject] IN ( [数学],[英语],[语文] ) ) AS pvt

  ORDER BY pvt.[UserName];

  GO

  (图4)

  (五) 把上面静态的SQL基础上进行修改,这样就不用理会记录里面存储了什么,需要转成什么列名的问题了,脚本如下,效果如图4所示:

  --4:动态PIVOT行转列

  DECLARE @sql_str VARCHAR(8000)

  DECLARE @sql_col VARCHAR(8000)

  SELECT @sql_col = ISNULL(@sql_col + ',','') + QUOTENAME([Subject]) FROM [TestRows2Columns] GROUP BY [Subject]

  SET @sql_str = '

  SELECT * FROM (

  SELECT [UserName],[Subject],[Source] FROM [TestRows2Columns]) p PIVOT

  (SUM([Source]) FOR [Subject] IN ( '+ @sql_col +') ) AS pvt

  ORDER BY pvt.[UserName]'

  PRINT (@sql_str)

  EXEC (@sql_str)

  (六) 也许很多人到了上面一步就够了,但是你会发现,当别人拿到你的代码,需要不断的修改成他自己环境中表名、分组列、行转列字段、字段值这几个参数,逻辑如图5所示,所以,我继续对上面的脚本进行修改,你只要设置自己的参数就可以实现行转列了,效果如图4所示:

  --5:参数化动态PIVOT行转列

  -- =============================================

  -- Author:

  -- Create date:

  -- Description:

  -- Blog:

  -- =============================================

  DECLARE @sql_str NVARCHAR(MAX)

  DECLARE @sql_col NVARCHAR(MAX)

  DECLARE @tableName SYSNAME --行转列表

  DECLARE @groupColumn SYSNAME --分组字段

  DECLARE @row2column SYSNAME --行变列的字段

  DECLARE @row2columnValue SYSNAME --行变列值的字段

  SET @tableName = 'TestRows2Columns'

  SET @groupColumn = 'UserName'

  SET @row2column = 'Subject'

  SET @row2columnValue = 'Source'

  --从行数据中获取可能存在的列

  SET @sql_str = N'

  SELECT @sql_col_out = ISNULL(@sql_col_out + '','','''') + QUOTENAME(['+@row2column+'])

  FROM ['+@tableName+'] GROUP BY ['+@row2column+']'

  --PRINT @sql_str

  EXEC sp_executesql @sql_str,N'@sql_col_out NVARCHAR(MAX) OUTPUT',@sql_col_out=@sql_col OUTPUT

  --PRINT @sql_col

  SET @sql_str = N'

  SELECT * FROM (

  SELECT ['+@groupColumn+'],['+@row2column+'],['+@row2columnValue+'] FROM ['+@tableName+']) p PIVOT

  (SUM(['+@row2columnValue+']) FOR ['+@row2column+'] IN ( '+ @sql_col +') ) AS pvt

  ORDER BY pvt.['+@groupColumn+']'

  --PRINT (@sql_str)

  EXEC (@sql_str)

wps_clip_image-17757

  (图5)

  (七) 在实际的运用中,我经常遇到需要对基础表的数据进行筛选后再进行行转列,那么下面的脚本将满足你这个需求,效果如图6所示:

  --6:带条件查询的参数化动态PIVOT行转列

  -- =============================================

  -- Author:

  -- Create date:

  -- Description:

  -- Blog:

  -- =============================================

  DECLARE @sql_str NVARCHAR(MAX)

  DECLARE @sql_col NVARCHAR(MAX)

  DECLARE @sql_where NVARCHAR(MAX)

  DECLARE @tableName SYSNAME --行转列表

  DECLARE @groupColumn SYSNAME --分组字段

  DECLARE @row2column SYSNAME --行变列的字段

  DECLARE @row2columnValue SYSNAME --行变列值的字段

  SET @tableName = 'TestRows2Columns'

  SET @groupColumn = 'UserName'

  SET @row2column = 'Subject'

  SET @row2columnValue = 'Source'

  SET @sql_where = 'WHERE UserName = ''王五'''

  --从行数据中获取可能存在的列

  SET @sql_str = N'

  SELECT @sql_col_out = ISNULL(@sql_col_out + '','','''') + QUOTENAME(['+@row2column+'])

  FROM ['+@tableName+'] '+@sql_where+' GROUP BY ['+@row2column+']'

  --PRINT @sql_str

  EXEC sp_executesql @sql_str,N'@sql_col_out NVARCHAR(MAX) OUTPUT',@sql_col_out=@sql_col OUTPUT

  --PRINT @sql_col

  SET @sql_str = N'

  SELECT * FROM (

  SELECT ['+@groupColumn+'],['+@row2column+'],['+@row2columnValue+'] FROM ['+@tableName+']'+@sql_where+') p PIVOT

  (SUM(['+@row2columnValue+']) FOR ['+@row2column+'] IN ( '+ @sql_col +') ) AS pvt

  ORDER BY pvt.['+@groupColumn+']'

  --PRINT (@sql_str)

  EXEC (@sql_str)

  (图6)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function