mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
174 lines
6.1 KiB
Transact-SQL
174 lines
6.1 KiB
Transact-SQL
-- ******************************************************** --
|
|
-- 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
|
|
|
|
-- Email IntelligentQP@microsoft.com for questions\feedback
|
|
-- ******************************************************** --
|
|
|
|
/*
|
|
Create MSTVF
|
|
*/
|
|
|
|
USE [WideWorldImportersDW];
|
|
GO
|
|
|
|
CREATE OR ALTER FUNCTION [Fact].[ufn_WhatIfOutlierEventQuantity](@event VARCHAR(15), @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]
|
|
|
|
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
|
|
|
|
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
|
|
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
|
|
|
|
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
|
|
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
|