-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathIQP Demo - Interleaved Execution.sql
More file actions
173 lines (152 loc) · 6.16 KB
/
IQP Demo - Interleaved Execution.sql
File metadata and controls
173 lines (152 loc) · 6.16 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
-- ******************************************************** --
-- Interleaved Execution
-- See https://aka.ms/IQP for more background
-- Demo scripts: https://aka.ms/IQPDemos
-- This demo is on SQL Server 2017 and Azure SQL DB
-- Last updated 1/29/2020 (Credit: Milos Radivojevic)
-- Changed @event to varchar(30) from varchar(15)
-- Added date range to 'Mild Recession' branch
-- Email [email protected] for questions\feedback
-- ******************************************************** --
/*
Create MSTVF
*/
USE [WideWorldImportersDW];
GO
CREATE OR ALTER FUNCTION [Fact].[ufn_WhatIfOutlierEventQuantity]
(@event VARCHAR(30), @beginOrderDateKey DATE, @endOrderDateKey DATE)
RETURNS @OutlierEventQuantity TABLE (
[Order Key] [bigint],
[City Key] [int] NOT NULL,
[Customer Key] [int] NOT NULL,
[Stock Item Key] [int] NOT NULL,
[Order Date Key] [date] NOT NULL,
[Picked Date Key] [date] NULL,
[Salesperson Key] [int] NOT NULL,
[Picker Key] [int] NULL,
[OutlierEventQuantity] [int] NOT NULL)
AS
BEGIN
-- Valid @event values
-- 'Mild Recession'
-- 'Hurricane - South Atlantic'
-- 'Hurricane - East South Central'
-- 'Hurricane - West South Central'
IF @event = 'Mild Recession'
INSERT @OutlierEventQuantity
SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key],
[o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key],
[o].[Salesperson Key], [o].[Picker Key],
CASE
WHEN [o].[Quantity] > 2 THEN [o].[Quantity] * .5
ELSE [o].[Quantity]
END
FROM [Fact].[Order] AS [o]
INNER JOIN [Dimension].[City] AS [c]
ON [c].[City Key] = [o].[City Key]
WHERE [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey
IF @event = 'Hurricane - South Atlantic'
INSERT @OutlierEventQuantity
SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key],
[o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key],
[o].[Salesperson Key], [o].[Picker Key],
CASE
WHEN [o].[Quantity] > 10 THEN [o].[Quantity] * .5
ELSE [o].[Quantity]
END
FROM [Fact].[Order] AS [o]
INNER JOIN [Dimension].[City] AS [c]
ON [c].[City Key] = [o].[City Key]
WHERE [c].[State Province] IN
('Florida', 'Georgia', 'Maryland', 'North Carolina',
'South Carolina', 'Virginia', 'West Virginia',
'Delaware')
AND [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey
IF @event = 'Hurricane - East South Central'
INSERT @OutlierEventQuantity
SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key],
[o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key],
[o].[Salesperson Key], [o].[Picker Key],
CASE
WHEN [o].[Quantity] > 50 THEN [o].[Quantity] * .5
ELSE [o].[Quantity]
END
FROM [Fact].[Order] AS [o]
INNER JOIN [Dimension].[City] AS [c]
ON [c].[City Key] = [o].[City Key]
INNER JOIN [Dimension].[Stock Item] AS [si]
ON [si].[Stock Item Key] = [o].[Stock Item Key]
WHERE [c].[State Province] IN
('Alabama', 'Kentucky', 'Mississippi', 'Tennessee')
AND [si].[Buying Package] = 'Carton'
AND [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey
IF @event = 'Hurricane - West South Central'
INSERT @OutlierEventQuantity
SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key],
[o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key],
[o].[Salesperson Key], [o].[Picker Key],
CASE
WHEN [cu].[Customer] = 'Unknown' THEN 0
WHEN [cu].[Customer] <> 'Unknown' AND
[o].[Quantity] > 10 THEN [o].[Quantity] * .5
ELSE [o].[Quantity]
END
FROM [Fact].[Order] AS [o]
INNER JOIN [Dimension].[City] AS [c]
ON [c].[City Key] = [o].[City Key]
INNER JOIN [Dimension].[Customer] AS [cu]
ON [cu].[Customer Key] = [o].[Customer Key]
WHERE [c].[State Province] IN
('Arkansas', 'Louisiana', 'Oklahoma', 'Texas')
AND [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey
RETURN
END
GO
USE [master];
GO
ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 130;
GO
USE [WideWorldImportersDW];
GO
SELECT [fo].[Order Key], [fo].[Description], [fo].[Package],
[fo].[Quantity], [foo].[OutlierEventQuantity]
FROM [Fact].[Order] AS [fo]
INNER JOIN [Fact].[ufn_WhatIfOutlierEventQuantity]('Mild Recession',
'1-01-2013',
'10-15-2014') AS [foo] ON [fo].[Order Key] = [foo].[Order Key]
AND [fo].[City Key] = [foo].[City Key]
AND [fo].[Customer Key] = [foo].[Customer Key]
AND [fo].[Stock Item Key] = [foo].[Stock Item Key]
AND [fo].[Order Date Key] = [foo].[Order Date Key]
AND [fo].[Picked Date Key] = [foo].[Picked Date Key]
AND [fo].[Salesperson Key] = [foo].[Salesperson Key]
AND [fo].[Picker Key] = [foo].[Picker Key]
INNER JOIN [Dimension].[Stock Item] AS [si]
ON [fo].[Stock Item Key] = [si].[Stock Item Key]
WHERE [si].[Lead Time Days] > 0
AND [fo].[Quantity] > 50;
GO
USE [master];
GO
ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 140;
GO
USE [WideWorldImportersDW];
GO
SELECT [fo].[Order Key], [fo].[Description], [fo].[Package],
[fo].[Quantity], [foo].[OutlierEventQuantity]
FROM [Fact].[Order] AS [fo]
INNER JOIN [Fact].[ufn_WhatIfOutlierEventQuantity]('Mild Recession',
'1-01-2013',
'10-15-2014') AS [foo] ON [fo].[Order Key] = [foo].[Order Key]
AND [fo].[City Key] = [foo].[City Key]
AND [fo].[Customer Key] = [foo].[Customer Key]
AND [fo].[Stock Item Key] = [foo].[Stock Item Key]
AND [fo].[Order Date Key] = [foo].[Order Date Key]
AND [fo].[Picked Date Key] = [foo].[Picked Date Key]
AND [fo].[Salesperson Key] = [foo].[Salesperson Key]
AND [fo].[Picker Key] = [foo].[Picker Key]
INNER JOIN [Dimension].[Stock Item] AS [si]
ON [fo].[Stock Item Key] = [si].[Stock Item Key]
WHERE [si].[Lead Time Days] > 0
AND [fo].[Quantity] > 50;
GO