SQL Server中使用PIVOT行转列

时间:2023-03-09 14:20:55
SQL Server中使用PIVOT行转列

使用PIVOT行转列

1.建表及插入数据

 USE [AdventureDB]
GO
/****** Object: Table [dbo].[Score] Script Date: 11/25/2016 4:30:50 PM ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Score]([Name] [varchar](50) NULL,[Subject] [varchar](50) NULL,[Score] FLOAT NULL) ON [PRIMARY]
GO INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Jack', N'linguistic', 65)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Tom', N'linguistic', 56)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Lina', N'linguistic', 84)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Jack', N'Mathematics', 100)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Tom', N'Mathematics', 82)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Lina', N'Mathematics', 67)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Jack', N'English', 82)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Tom', N'English', 54)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Lina', N'English', 76)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'James', N'Other', 52)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Tom', N'Other', 99)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Lina', N'Other', 79)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kobe', N'linguistic', 65)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'James', N'linguistic', 76)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kidd', N'linguistic', 86)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'James', N'Mathematics', 70)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kobe', N'Mathematics', 92)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kidd', N'Mathematics', 70)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kobe', N'English', 86)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kidd', N'English', 85)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'James', N'English', 66)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Jack', N'Other', 77)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kobe', N'Other', 97)
INSERT INTO [dbo].[Score] ([Name], [Subject], [Score]) VALUES (N'Kidd', N'Other', 93)

SQL Server中使用PIVOT行转列

2.使用CASE语句查询

 USE [AdventureDB]
GO /****** Object: StoredProcedure [dbo].[CaseSelect] Script Date: 12/02/2016 00:47:02 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE procedure [dbo].[CaseSelect] AS BEGIN SELECT [Name],
SUM (case when [Subject] = 'English' then [Score] else 0 end) English,
SUM (case when [Subject] = 'linguistic' then [Score] else 0 end) Linguistic,
SUM (case when [Subject] = 'Mathematics' then [Score] else 0 end) Mathematics,
SUM (case when [Subject] = 'Other' then [Score] else 0 end) Other,
AVG ([Score]) Average
FROM [dbo].[score] GROUP BY [Name] ORDER BY [Name] DESC END GO

SQL Server中使用PIVOT行转列

3.使用PIVOT行转列

 USE [AdventureDB]
GO /****** Object: StoredProcedure [dbo].[Pivot] Script Date: 12/02/2016 01:07:27 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE procedure [dbo].[Pivot]
@NumberOfStudents int = 5
AS IF @NumberOfStudents < 1 or @NumberOfStudents > 10
RAISERROR('@NumberOfStudents must be between 1 and 10', 11, 1);
ELSE
SELECT top(@NumberOfStudents)
p.[name],
p.English,
p.linguistic,
p.Mathematics,
p.Other,
(p.English + p.linguistic+p.Mathematics + p.Other)/4 AS Average
FROM [dbo].[score] PIVOT (SUM (score) FOR [subject] IN (English,linguistic,Mathematics,Other) ) AS P
ORDER BY p.[name] DESC RETURN; GO

4.PIVOT动态获取列

 USE [AdventureDB]
GO /****** Object: StoredProcedure [dbo].[Pivot_DynamicColumn] Script Date: 12/02/2016 01:31:30 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE procedure [dbo].[Pivot_DynamicColumn] AS BEGIN
DECLARE @ColumnNames NVARCHAR(Max)
DECLARE @AverageScore NVARCHAR(Max)
DECLARE @ColumnCount int SET @ColumnNames=''
SET @AverageScore = ''
SET @ColumnCount = '' SELECT @ColumnCount = COUNT(DISTINCT [Subject]) FROM [Score] SELECT
@ColumnNames = @ColumnNames + '[' + [Subject] + '],',
@AverageScore = @AverageScore + '[' + [Subject] + ']+'
FROM
(
SELECT DISTINCT [Subject] FROM [Score]
) t SET @ColumnNames= LEFT(@ColumnNames, LEN(@ColumnNames)-1)
SET @AverageScore= LEFT(@AverageScore, LEN(@AverageScore)-1) DECLARE @selectSQL NVARCHAR(Max) SET @selectSQL=
'SELECT [name],{0},({1})/{2} as Average FROM
[dbo].[score]
Pivot(SUM(score) For [subject] in ({0})) AS p
ORDER BY p.[name] DESC' SET @selectSQL= REPLACE(@selectSQL,'{0}',@ColumnNames)
SET @selectSQL= REPLACE(@selectSQL,'{1}',@AverageScore)
SET @selectSQL= REPLACE(@selectSQL,'{2}',@ColumnCount) EXEC sp_executesql @selectSQL
END GO

使用UNPIVOT列转行

1.建表及插入数据

 USE [AdventureDB]
GO /****** Object: Table [dbo].[ScorePivot] Script Date: 2016/12/6 17:38:48 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[ScorePivot](
[Name] [varchar](50) NULL,
[English] [varchar](50) NULL,
[Linguistic] [varchar](50) NULL,
[Mathematics] [varchar](50) NULL,
[Other] [varchar](50) NULL) ON [PRIMARY]
GO INSERT INTO [dbo].[ScorePivot] ([Name], [English], [Linguistic], [Mathematics], [Other]) VALUES (N'Tom', N'', N'', N'', N'')
INSERT INTO [dbo].[ScorePivot] ([Name], [English], [Linguistic], [Mathematics], [Other]) VALUES (N'Lina', N'', N'', N'', N'')
INSERT INTO [dbo].[ScorePivot] ([Name], [English], [Linguistic], [Mathematics], [Other]) VALUES (N'Kobe', N'', N'', N'', N'')
INSERT INTO [dbo].[ScorePivot] ([Name], [English], [Linguistic], [Mathematics], [Other]) VALUES (N'Kidd', N'', N'', N'', N'')
INSERT INTO [dbo].[ScorePivot] ([Name], [English], [Linguistic], [Mathematics], [Other]) VALUES (N'James', N'', N'', N'', N'')
INSERT INTO [dbo].[ScorePivot] ([Name], [English], [Linguistic], [Mathematics], [Other]) VALUES (N'Jack', N'', N'', N'', N'')

SQL Server中使用PIVOT行转列

2.使用UNPIVOT列转行

USE [AdventureDB]
GO /****** Object: StoredProcedure [dbo].[UNPivot] Script Date: 2016/12/6 17:49:54 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE procedure [dbo].[UNPivot] AS SELECT
[Name], [Subject], [Score]
FROM
(
SELECT [Name], [English],[Linguistic],[Mathematics], [Other] FROM [dbo].[ScorePivot]
) data
UNPIVOT
(
[Score] FOR [Subject] IN
(
[English], [Linguistic], [Mathematics], [Other]
)
)AS nupvt GO

SQL Server中使用PIVOT行转列