当前位置:首页 > 数据库 > Sqlserver

SQL Server中使用PIVOT行转列

1.建表及插入数据

技术分享 技术分享
                 1
                USE
                [
                AdventureDB
                ]
                 2
                GO
                 3
                /*
                ***** Object:  Table [dbo].[Score]    Script Date: 11/25/2016 4:30:50 PM *****
                */
                 4
                SET ANSI_NULLS ON 5GO 6 7SET QUOTED_IDENTIFIER ON 8GO 910CREATETABLE[dbo].[Score]([Name][varchar](50) NULL,[Subject][varchar](50) NULL,[Score]FLOATNULL) ON[PRIMARY]11GO1213INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJack, Nlinguistic, 65)
14INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NTom, Nlinguistic, 56)
15INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NLina, Nlinguistic, 84)
16INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJack, NMathematics, 100)
17INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NTom, NMathematics, 82)
18INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NLina, NMathematics, 67)
19INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJack, NEnglish, 82)
20INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NTom, NEnglish, 54)
21INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NLina, NEnglish, 76)
22INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJames, NOther, 52)
23INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NTom, NOther, 99)
24INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NLina, NOther, 79)
25INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKobe, Nlinguistic, 65)
26INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJames, Nlinguistic, 76)
27INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKidd, Nlinguistic, 86)
28INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJames, NMathematics, 70)
29INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKobe, NMathematics, 92)
30INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKidd, NMathematics, 70)
31INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKobe, NEnglish, 86)
32INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKidd, NEnglish, 85)
33INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJames, NEnglish, 66)
34INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NJack, NOther, 77)
35INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKobe, NOther, 97)
36INSERTINTO[dbo].[Score] ([Name], [Subject], [Score]) VALUES (NKidd, NOther, 93)
View Code

技术分享

2.使用CASE语句查询

技术分享 技术分享
                 1
                USE
                [
                AdventureDB
                ]
                 2
                GO
                 3
                 4
                /*
                ***** Object:  StoredProcedure [dbo].[CaseSelect]    Script Date: 12/02/2016 00:47:02 *****
                */
                 5
                SET ANSI_NULLS ON 6GO 7 8SET QUOTED_IDENTIFIER ON 9GO1011CREATEprocedure[dbo].[CaseSelect]AS1213BEGIN1415SELECT[Name],
16SUM (casewhen[Subject]=Englishthen[Score]else0end) English,
17SUM (casewhen[Subject]=linguisticthen[Score]else0end) Linguistic,
18SUM (casewhen[Subject]=Mathematicsthen[Score]else0end) Mathematics,
19SUM (casewhen[Subject]=Otherthen[Score]else0end) Other,
20AVG ([Score]) Average
21FROM[dbo].[score]GROUPBY[Name]ORDERBY[Name]DESC2223END2425GO
View Code

技术分享

3.使用PIVOT行转列

技术分享 技术分享
                 1
                USE
                [
                AdventureDB
                ]
                 2
                GO
                 3
                 4
                /*
                ***** Object:  StoredProcedure [dbo].[Pivot]    Script Date: 12/02/2016 01:07:27 *****
                */
                 5
                SET ANSI_NULLS ON 6GO 7 8SET QUOTED_IDENTIFIER ON 9GO1011CREATEprocedure[dbo].[Pivot]12@NumberOfStudentsint=513AS1415IF@NumberOfStudents<1or@NumberOfStudents>1016RAISERROR(@NumberOfStudents must be between 1 and 10, 11, 1);
17ELSE18SELECTtop(@NumberOfStudents)
19         p.[name],
20        p.English,
21        p.linguistic,
22        p.Mathematics,
23        p.Other,
24         (p.English + p.linguistic+p.Mathematics + p.Other)/4AS Average
25FROM[dbo].[score] PIVOT (SUM (score) FOR[subject]IN (English,linguistic,Mathematics,Other) ) AS P
26ORDERBY  p.[name]DESC2728RETURN;
2930GO
View Code

4.PIVOT动态获取列

技术分享 技术分享
                 1
                USE
                [
                AdventureDB
                ]
                 2
                GO
                 3
                 4
                /*
                ***** Object:  StoredProcedure [dbo].[Pivot_DynamicColumn]    Script Date: 12/02/2016 01:31:30 *****
                */
                 5
                SET ANSI_NULLS ON 6GO 7 8SET QUOTED_IDENTIFIER ON 9GO1011CREATEprocedure[dbo].[Pivot_DynamicColumn]AS1213BEGIN14DECLARE@ColumnNamesNVARCHAR(Max)
15DECLARE@AverageScoreNVARCHAR(Max)
16DECLARE@ColumnCountint1718SET@ColumnNames=‘‘19SET@AverageScore=‘‘20SET@ColumnCount=‘‘2122SELECT@ColumnCount=COUNT(DISTINCT[Subject]) FROM[Score]2324SELECT25@ColumnNames=@ColumnNames+[+[Subject]+],,
26@AverageScore=@AverageScore+[+[Subject]+]+27FROM28       (
29SELECTDISTINCT[Subject]FROM[Score]30       ) t
3132SET@ColumnNames=LEFT(@ColumnNames, LEN(@ColumnNames)-1)
33SET@AverageScore=LEFT(@AverageScore, LEN(@AverageScore)-1)
3435DECLARE@selectSQLNVARCHAR(Max)
3637SET@selectSQL=38SELECT [name],{0},({1})/{2} as Average FROM
39       [dbo].[score]
40     Pivot(SUM(score) For [subject] in ({0})) AS p
41       ORDER BY  p.[name] DESC4243SET@selectSQL=REPLACE(@selectSQL,{0},@ColumnNames)
44SET@selectSQL=REPLACE(@selectSQL,{1},@AverageScore)
45SET@selectSQL=REPLACE(@selectSQL,{2},@ColumnCount)
4647EXEC sp_executesql @selectSQL48END4950GO
View Code

5.使用UNPIVOT列转行(待续)

 

原文:http://www.cnblogs.com/makesense/p/6124282.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!