-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-pivot.sql
154 lines (124 loc) · 3.18 KB
/
dynamic-pivot.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
USE TestDB
GO
-- Unpivoting data and inserting into a new table.
SELECT
Company -- This is the column we already had.
, [Year] -- We're creating this column
, [Revenue] -- And this column from the values we are unpivoting.
INTO CompanyDataTransactions
FROM
CompanyData
UNPIVOT
(
-- Name we are giving this name to the column we're creating.
[Revenue]
-- Set of columns that we are unpivoting
FOR [Year] IN ([2012],[2013],[2014],[2015],[2016],[2017],[2018],[2019])
) AS u;
-- Insert more data to our transactions table
INSERT INTO CompanyDataTransactions
SELECT 'SQL Inc', 2011, 766843 UNION ALL
SELECT 'SQL Inc', 2020, 974278 UNION ALL
SELECT 'Facenotebook', 2011, 556322 UNION ALL
SELECT 'Facenotebook', 2020, 993721 UNION ALL
SELECT 'Twister', 2011, 553098 UNION ALL
SELECT 'Twister', 2020, 785986 UNION ALL
SELECT 'Yulp', 2011, 387926 UNION ALL
SELECT 'Yulp', 2020, 968301 UNION ALL
SELECT 'JourneyAdvisor', 2011, 548769 UNION ALL
SELECT 'JourneyAdvisor', 2020, 849210
-- Dynamic PIVOT
DECLARE @columns NVARCHAR(1000);
-- Create a list of distinct values that will become our columns.
SELECT @columns = STRING_AGG([Year], ',') FROM
(
SELECT DISTINCT QUOTENAME([Year]) AS [Year]
FROM CompanyDataTransactions
) C;
-- Now, we need to write our PIVOT statement the same way we always do it.
DECLARE @pivot_stmt NVARCHAR(MAX);
SET @pivot_stmt =
'
SELECT
*
FROM
(
SELECT
Company
, [Year]
, Revenue
FROM
CompanyDataTransactions
) AS CompanyData
PIVOT
(
MAX(Revenue)
FOR [Year] IN (
'
-- Add the colums in our list
SELECT @pivot_stmt = @pivot_stmt + (SELECT @columns) + ')) AS P;'
EXEC sp_executesql @pivot_stmt
-- Dynamic PIVOT alternatives
-- Using a table variable
DECLARE @columns TABLE
(
column_name NVARCHAR(200)
);
-- Create a list of distinct values that will become our columns.
INSERT INTO @columns
SELECT DISTINCT [Year] AS [Year]
FROM CompanyDataTransactions;
-- Now, we need to write our PIVOT statement the same way we always do it.
DECLARE @pivot_stmt NVARCHAR(MAX);
SET @pivot_stmt =
'
SELECT
*
FROM
(
SELECT
Company
, [Year]
, Revenue
FROM
CompanyDataTransactions
) AS CompanyData
PIVOT
(
MAX(Revenue)
FOR [Year] IN (
'
-- Add the colums as a comma separated list.
SELECT @pivot_stmt = @pivot_stmt + '[' + column_name + '],'
FROM @columns;
-- We need to remove the last comma from the list.
SELECT @pivot_stmt = SUBSTRING(@pivot_stmt, 1, LEN(@pivot_stmt)-1);
SELECT @pivot_stmt = @pivot_stmt + ')) AS P;'
EXEC sp_executesql @pivot_stmt
-- Adding the values directly in the dynamic statement.
-- Here, we just need to write our PIVOT statement the same way we always do it.
DECLARE @pivot_stmt NVARCHAR(MAX);
SET @pivot_stmt =
'
SELECT
*
FROM
(
SELECT
Company
, [Year]
, Revenue
FROM
CompanyDataTransactions
) AS CompanyData
PIVOT
(
MAX(Revenue)
FOR [Year] IN (
'
-- Add the colums as a comma separated list querying directly from the table.
SELECT @pivot_stmt = @pivot_stmt + '[' + [Year] + '],' FROM CompanyDataTransactions GROUP BY [Year]
-- We need to remove the last comma from the list.
SELECT @pivot_stmt = SUBSTRING(@pivot_stmt, 1, LEN(@pivot_stmt)-1);
SELECT @pivot_stmt = @pivot_stmt + ')) AS P;'
EXEC sp_executesql @pivot_stmt