SQL scripts moved into sql-scripts folder

Added demo.sql. setup.sql is moved from setup to sql-scripts folder.
This commit is contained in:
Jovan Popovic
2016-08-02 10:30:02 -07:00
parent badb1971d8
commit b4a25e6138
2 changed files with 75 additions and 1 deletions
@@ -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
@@ -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 (