mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
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:
@@ -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
|
||||
+13
-1
@@ -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 (
|
||||
Reference in New Issue
Block a user