|
| 1 | +-- ******************************************************** -- |
| 2 | +-- Interleaved Execution |
| 3 | + |
| 4 | +-- See https://aka.ms/IQP for more background |
| 5 | + |
| 6 | +-- Demo scripts: https://aka.ms/IQPDemos |
| 7 | + |
| 8 | +-- This demo is on SQL Server 2017 and Azure SQL DB |
| 9 | + |
| 10 | +-- Email IntelligentQP@microsoft.com for questions\feedback |
| 11 | +-- ******************************************************** -- |
| 12 | + |
| 13 | +/* |
| 14 | + Create MSTVF |
| 15 | +*/ |
| 16 | + |
| 17 | +USE [WideWorldImportersDW]; |
| 18 | +GO |
| 19 | + |
| 20 | +CREATE FUNCTION [Fact].[WhatIfOutlierEventQuantity](@event VARCHAR(15), @beginOrderDateKey DATE, @endOrderDateKey DATE) |
| 21 | +RETURNS @OutlierEventQuantity TABLE ( |
| 22 | + [Order Key] [bigint], |
| 23 | + [City Key] [int] NOT NULL, |
| 24 | + [Customer Key] [int] NOT NULL, |
| 25 | + [Stock Item Key] [int] NOT NULL, |
| 26 | + [Order Date Key] [date] NOT NULL, |
| 27 | + [Picked Date Key] [date] NULL, |
| 28 | + [Salesperson Key] [int] NOT NULL, |
| 29 | + [Picker Key] [int] NULL, |
| 30 | + [OutlierEventQuantity] [int] NOT NULL) |
| 31 | +AS |
| 32 | +BEGIN |
| 33 | + |
| 34 | +-- Valid @event values |
| 35 | + -- 'Mild Recession' |
| 36 | + -- 'Hurricane - South Atlantic' |
| 37 | + -- 'Hurricane - East South Central' |
| 38 | + -- 'Hurricane - West South Central' |
| 39 | + IF @event = 'Mild Recession' |
| 40 | + INSERT @OutlierEventQuantity |
| 41 | + SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key], |
| 42 | + [o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key], |
| 43 | + [o].[Salesperson Key], [o].[Picker Key], |
| 44 | + CASE |
| 45 | + WHEN [o].[Quantity] > 2 THEN [o].[Quantity] * .5 |
| 46 | + ELSE [o].[Quantity] |
| 47 | + END |
| 48 | + FROM [Fact].[Order] AS [o] |
| 49 | + INNER JOIN [Dimension].[City] AS [c] |
| 50 | + ON [c].[City Key] = [o].[City Key] |
| 51 | + |
| 52 | + IF @event = 'Hurricane - South Atlantic' |
| 53 | + INSERT @OutlierEventQuantity |
| 54 | + SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key], |
| 55 | + [o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key], |
| 56 | + [o].[Salesperson Key], [o].[Picker Key], |
| 57 | + CASE |
| 58 | + WHEN [o].[Quantity] > 10 THEN [o].[Quantity] * .5 |
| 59 | + ELSE [o].[Quantity] |
| 60 | + END |
| 61 | + FROM [Fact].[Order] AS [o] |
| 62 | + INNER JOIN [Dimension].[City] AS [c] |
| 63 | + ON [c].[City Key] = [o].[City Key] |
| 64 | + WHERE [c].[State Province] IN |
| 65 | + ('Florida', 'Georgia', 'Maryland', 'North Carolina', |
| 66 | + 'South Carolina', 'Virginia', 'West Virginia', |
| 67 | + 'Delaware') |
| 68 | + AND [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey |
| 69 | + |
| 70 | + IF @event = 'Hurricane - East South Central' |
| 71 | + INSERT @OutlierEventQuantity |
| 72 | + SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key], |
| 73 | + [o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key], |
| 74 | + [o].[Salesperson Key], [o].[Picker Key], |
| 75 | + CASE |
| 76 | + WHEN [o].[Quantity] > 50 THEN [o].[Quantity] * .5 |
| 77 | + ELSE [o].[Quantity] |
| 78 | + END |
| 79 | + FROM [Fact].[Order] AS [o] |
| 80 | + INNER JOIN [Dimension].[City] AS [c] |
| 81 | + ON [c].[City Key] = [o].[City Key] |
| 82 | + INNER JOIN [Dimension].[Stock Item] AS [si] |
| 83 | + ON [si].[Stock Item Key] = [o].[Stock Item Key] |
| 84 | + WHERE [c].[State Province] IN |
| 85 | + ('Alabama', 'Kentucky', 'Mississippi', 'Tennessee') |
| 86 | + AND [si].[Buying Package] = 'Carton' |
| 87 | + AND [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey |
| 88 | + |
| 89 | + IF @event = 'Hurricane - West South Central' |
| 90 | + INSERT @OutlierEventQuantity |
| 91 | + SELECT [o].[Order Key], [o].[City Key], [o].[Customer Key], |
| 92 | + [o].[Stock Item Key], [o].[Order Date Key], [o].[Picked Date Key], |
| 93 | + [o].[Salesperson Key], [o].[Picker Key], |
| 94 | + CASE |
| 95 | + WHEN [cu].[Customer] = 'Unknown' THEN 0 |
| 96 | + WHEN [cu].[Customer] <> 'Unknown' AND |
| 97 | + [o].[Quantity] > 10 THEN [o].[Quantity] * .5 |
| 98 | + ELSE [o].[Quantity] |
| 99 | + END |
| 100 | + FROM [Fact].[Order] AS [o] |
| 101 | + INNER JOIN [Dimension].[City] AS [c] |
| 102 | + ON [c].[City Key] = [o].[City Key] |
| 103 | + INNER JOIN [Dimension].[Customer] AS [cu] |
| 104 | + ON [cu].[Customer Key] = [o].[Customer Key] |
| 105 | + WHERE [c].[State Province] IN |
| 106 | + ('Arkansas', 'Louisiana', 'Oklahoma', 'Texas') |
| 107 | + AND [o].[Order Date Key] BETWEEN @beginOrderDateKey AND @endOrderDateKey |
| 108 | + |
| 109 | + RETURN |
| 110 | +END |
| 111 | +GO |
| 112 | + |
| 113 | +USE [master]; |
| 114 | +GO |
| 115 | + |
| 116 | +ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 130; |
| 117 | +GO |
| 118 | + |
| 119 | +ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE; |
| 120 | +GO |
| 121 | + |
| 122 | +USE [WideWorldImportersDW]; |
| 123 | +GO |
| 124 | + |
| 125 | +SELECT [fo].[Order Key], [fo].[Description], [fo].[Package], |
| 126 | + [fo].[Quantity], [foo].[OutlierEventQuantity] |
| 127 | +FROM [Fact].[Order] AS [fo] |
| 128 | +INNER JOIN [Fact].[WhatIfOutlierEventQuantity]('Mild Recession', |
| 129 | + '1-01-2013', |
| 130 | + '10-15-2014') AS [foo] ON [fo].[Order Key] = [foo].[Order Key] |
| 131 | + AND [fo].[City Key] = [foo].[City Key] |
| 132 | + AND [fo].[Customer Key] = [foo].[Customer Key] |
| 133 | + AND [fo].[Stock Item Key] = [foo].[Stock Item Key] |
| 134 | + AND [fo].[Order Date Key] = [foo].[Order Date Key] |
| 135 | + AND [fo].[Picked Date Key] = [foo].[Picked Date Key] |
| 136 | + AND [fo].[Salesperson Key] = [foo].[Salesperson Key] |
| 137 | + AND [fo].[Picker Key] = [foo].[Picker Key] |
| 138 | +INNER JOIN [Dimension].[Stock Item] AS [si] |
| 139 | + ON [fo].[Stock Item Key] = [si].[Stock Item Key] |
| 140 | +WHERE [si].[Lead Time Days] > 0 |
| 141 | + AND [fo].[Quantity] > 50; |
| 142 | +GO |
| 143 | + |
| 144 | +USE [master]; |
| 145 | +GO |
| 146 | + |
| 147 | +ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 140; |
| 148 | +GO |
| 149 | + |
| 150 | +ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE; |
| 151 | +GO |
| 152 | + |
| 153 | +USE [WideWorldImportersDW]; |
| 154 | +GO |
| 155 | + |
| 156 | +SELECT [fo].[Order Key], [fo].[Description], [fo].[Package], |
| 157 | + [fo].[Quantity], [foo].[OutlierEventQuantity] |
| 158 | +FROM [Fact].[Order] AS [fo] |
| 159 | +INNER JOIN [Fact].[WhatIfOutlierEventQuantity]('Mild Recession', |
| 160 | + '1-01-2013', |
| 161 | + '10-15-2014') AS [foo] ON [fo].[Order Key] = [foo].[Order Key] |
| 162 | + AND [fo].[City Key] = [foo].[City Key] |
| 163 | + AND [fo].[Customer Key] = [foo].[Customer Key] |
| 164 | + AND [fo].[Stock Item Key] = [foo].[Stock Item Key] |
| 165 | + AND [fo].[Order Date Key] = [foo].[Order Date Key] |
| 166 | + AND [fo].[Picked Date Key] = [foo].[Picked Date Key] |
| 167 | + AND [fo].[Salesperson Key] = [foo].[Salesperson Key] |
| 168 | + AND [fo].[Picker Key] = [foo].[Picker Key] |
| 169 | +INNER JOIN [Dimension].[Stock Item] AS [si] |
| 170 | + ON [fo].[Stock Item Key] = [si].[Stock Item Key] |
| 171 | +WHERE [si].[Lead Time Days] > 0 |
| 172 | + AND [fo].[Quantity] > 50; |
| 173 | +GO |
0 commit comments