From b4a25e6138f3e5cad9b37729fc8ce8a984594fd8 Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Tue, 2 Aug 2016 10:30:02 -0700 Subject: [PATCH] SQL scripts moved into sql-scripts folder Added demo.sql. setup.sql is moved from setup to sql-scripts folder. --- .../product-catalog/sql-scripts/demo.sql | 62 +++++++++++++++++++ .../{setup => sql-scripts}/setup.sql | 14 ++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 samples/features/temporal/product-catalog/sql-scripts/demo.sql rename samples/features/temporal/product-catalog/{setup => sql-scripts}/setup.sql (98%) diff --git a/samples/features/temporal/product-catalog/sql-scripts/demo.sql b/samples/features/temporal/product-catalog/sql-scripts/demo.sql new file mode 100644 index 00000000..9540ba4d --- /dev/null +++ b/samples/features/temporal/product-catalog/sql-scripts/demo.sql @@ -0,0 +1,62 @@ +--Note: Run setup.sql script to create ProductCatalog database. +USE ProductCatalog +GO + +-- Find the time when the first product(s) were inserted. +select min(ValidFrom) from History.Product; + +--List of current products. +select ProductID, Name, Color, Size, Price, Quantity +from Product; + +--Go back in time and show the list of products. +select ProductID, Name, Color, Size, Price, Quantity +from Product FOR SYSTEM_TIME AS OF '2015-07-28 13:20:00'; + +--Show the difference between current version of the product with ID 17 and version that existed in past. +select * +from dbo.diff_Product(17, '2015-07-28 13:20:00'); + +--Return full history of changes for product with ID 17. +select ProductID, Name, Color, Size, Price, Quantity, ValidFrom, ValidTo +from Product FOR SYSTEM_TIME ALL +where productid = 17 +order by ValidFrom desc; + +--Update product with ID 17. +update Product +set Color = 'Silver' +where productid = 17; + +--Verify that color is changed to 'Silver'. +select ProductID, Name, Color, Size, Price, Quantity, ValidFrom, ValidTo +from Product +where productid = 17; + +--Return history of the product. You should see one additional row in history. +select ProductID, Name, Color, Size, Price, Quantity, ValidFrom, ValidTo +from Product FOR SYSTEM_TIME ALL +where productid = 17 +order by ValidFrom desc; + +--Restore product. Overwrite the latest version with the verison that was valid last year. +exec RestoreProduct 17, '2015-11-07 03:40:09'; + +--Return history of the product with id 17 and verify that it current version and '2015-11-07 03:40:09' version are identical. +select ProductID, Name, Color, Size, Price, Quantity, ValidFrom, ValidTo +from Product FOR SYSTEM_TIME ALL +where productid = 17 +order by ValidFrom desc; + +-- Identify "spikes" in product price changes. +with history as ( + + select ProductID, Name, Price, ValidFrom, + LAG (Price, 1, 1) over (partition by ProductID order by ValidFrom) as PrevPrice, + LEAD (Price, 1, 1) over (partition by ProductID order by ValidFrom) as NextPrice + from dbo.Product for system_time all +) +select ValidFrom AS ModifiedOn, ProductID, Name, PrevPrice, Price, NextPrice +from history +where PrevPrice = NextPrice + AND ABS(PrevPrice - Price)/PrevPrice >=0.5 diff --git a/samples/features/temporal/product-catalog/setup/setup.sql b/samples/features/temporal/product-catalog/sql-scripts/setup.sql similarity index 98% rename from samples/features/temporal/product-catalog/setup/setup.sql rename to samples/features/temporal/product-catalog/sql-scripts/setup.sql index 23c83b74..b1016cc0 100644 --- a/samples/features/temporal/product-catalog/setup/setup.sql +++ b/samples/features/temporal/product-catalog/sql-scripts/setup.sql @@ -1,4 +1,16 @@ -CREATE SCHEMA History +USE master +GO + +DROP DATABASE IF EXISTS ProductCatalog +GO + +CREATE DATABASE ProductCatalog +GO + +USE ProductCatalog +GO + +CREATE SCHEMA History GO CREATE TABLE Product (