-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft: add notes + a start at some exercises
- Loading branch information
1 parent
ccae7dd
commit e89f196
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-- @conn xtdb | ||
-- Given the following data | ||
-- @block | ||
INSERT INTO InsurancePolicies ( | ||
_id, | ||
PolicyHolderName, | ||
PolicyType, | ||
PolicyDetails, | ||
_valid_from, | ||
_valid_to, | ||
PremiumAmount, | ||
CoverageAmount | ||
) VALUES | ||
('POL123456', 'John Doe', 'Health', {dob: DATE '1980-01-01'}, DATE '2024-01-01', DATE '2025-01-01', 1200.50, 100000.00), | ||
('POL123457', 'Jane Smith', 'Auto', {dob: DATE '1985-02-02'}, DATE '2024-02-01', DATE '2025-02-01', 900.75, 50000.00), | ||
('POL123458', 'Alice Johnson', 'Life', {dob: DATE '1970-03-03'}, DATE '2024-03-01', DATE '2044-03-01', 1500.00, 200000.00), | ||
('POL123459', 'Bob Brown', 'Home', {dob: DATE '1990-04-04'}, DATE '2024-04-01', DATE '2025-04-01', 800.00, 150000.00), | ||
('POL123460', 'Charlie Davis', 'Health', {dob: DATE '1982-05-05'}, DATE '2024-05-01', DATE '2025-05-01', 1100.25, 120000.00), | ||
('POL123461', 'Diana Evans', 'Auto', {dob: DATE '1988-06-06'}, DATE '2024-06-01', DATE '2025-06-01', 950.50, 60000.00), | ||
('POL123462', 'Evan Foster', 'Life', {dob: DATE '1975-07-07'}, DATE '2024-07-01', DATE '2044-07-01', 1600.75, 250000.00), | ||
('POL123463', 'Fiona Green', 'Home', {dob: DATE '1992-08-08'}, DATE '2024-08-01', DATE '2025-08-01', 850.00, 175000.00), | ||
('POL123464', 'George Harris', 'Health', {dob: DATE '1978-09-09'}, DATE '2024-09-01', DATE '2025-09-01', 1150.00, 130000.00), | ||
('POL123465', 'Hannah Jones', 'Auto', {dob: DATE '1983-10-10'}, DATE '2024-10-01', DATE '2025-10-01', 1000.00, 70000.00); | ||
|
||
-- Here's how you get the dob: | ||
-- @block | ||
SELECT _id, (PolicyDetails).dob | ||
FROM InsurancePolicies | ||
ORDER BY _id; | ||
|
||
-- Here's how you extract the month from a date: | ||
-- @block | ||
SELECT _id, EXTRACT(MONTH FROM _valid_from) | ||
FROM InsurancePolicies | ||
ORDER BY _id; | ||
|
||
-- Here's how you calcualte the interval between two dates: | ||
-- @block | ||
SELECT _id, AGE(_valid_to, _valid_from) | ||
FROM InsurancePolicies | ||
ORDER BY _id; | ||
|
||
-- Challenge: Calculate the current age in years of each policy owner | ||
-- @block | ||
SELECT _id, 40 AS years_old | ||
FROM InsurancePolicies | ||
ORDER BY _id; | ||
|
||
-- Answer: | ||
-- @block | ||
SELECT _id, EXTRACT(YEAR FROM AGE(CURRENT_DATE, (PolicyDetails).dob)) AS years_old | ||
FROM InsurancePolicies | ||
ORDER BY _id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
-- @conn xtdb | ||
|
||
-- Let's denormalise our Policies table and create a Users table: | ||
-- @block | ||
INSERT INTO Users (_id, UserName, DateOfBirth, _valid_from) VALUES | ||
(1, 'John Doe', DATE '1980-01-01', DATE '2024-01-01'), | ||
(2, 'Jane Smith', DATE '1985-02-02', DATE '2024-01-01'), | ||
(3, 'Alice Johnson', DATE '1970-03-03', DATE '2024-01-01'), | ||
(4, 'Bob Brown', DATE '1990-04-04', DATE '2024-01-01'), | ||
(5, 'Charlie Davis', DATE '1982-05-05', DATE '2024-01-01'), | ||
(6, 'Diana Evans', DATE '1988-06-06', DATE '2024-01-01'), | ||
(7, 'Evan Foster', DATE '1975-07-07', DATE '2024-01-01'), | ||
(8, 'Fiona Green', DATE '1992-08-08', DATE '2024-01-01'), | ||
(9, 'George Harris', DATE '1978-09-09', DATE '2024-01-01'), | ||
(10, 'Hannah Jones', DATE '1983-10-10', DATE '2024-01-01'); | ||
|
||
-- Our Policies table now looks like this: | ||
-- @block | ||
INSERT INTO Policies ( | ||
_id, | ||
UserId, | ||
PolicyType, | ||
_valid_from, | ||
_valid_to, | ||
PremiumAmount, | ||
CoverageAmount | ||
) VALUES | ||
('POL123456', 1, 'Health', DATE '2024-01-01', DATE '2025-01-01', 1200.50, 100000.00), | ||
('POL123457', 2, 'Auto', DATE '2024-02-01', DATE '2025-02-01', 900.75, 50000.00), | ||
('POL123458', 3, 'Life', DATE '2024-03-01', DATE '2044-03-01', 1500.00, 200000.00), | ||
('POL123459', 4, 'Home', DATE '2024-04-01', DATE '2025-04-01', 800.00, 150000.00), | ||
('POL123460', 5, 'Health', DATE '2024-05-01', DATE '2025-05-01', 1100.25, 120000.00), | ||
('POL123461', 6, 'Auto', DATE '2024-06-01', DATE '2025-06-01', 950.50, 60000.00), | ||
('POL123462', 7, 'Life', DATE '2024-07-01', DATE '2044-07-01', 1600.75, 250000.00), | ||
('POL123463', 8, 'Home', DATE '2024-08-01', DATE '2025-08-01', 850.00, 175000.00), | ||
('POL123464', 9, 'Health', DATE '2024-09-01', DATE '2025-09-01', 1150.00, 130000.00), | ||
('POL123465', 10, 'Auto', DATE '2024-10-01', DATE '2025-10-01', 1000.00, 70000.00); | ||
|
||
-- NOTE: this would be more interesing if it was something that would affect the premium | ||
-- Now, let's say a user got married on the 20th May 2024, how would you update the user table? | ||
-- @block | ||
UPDATE Users FOR PORTION OF VALID_TIME FROM DATE '2024-05-20' TO NULL | ||
SET UserName = 'Alice Williams' | ||
WHERE _id = 3; | ||
|
||
-- Now let's check that: | ||
-- @block | ||
SELECT * | ||
FROM Users | ||
ORDER BY _id; | ||
|
||
-- How can we get a complete view of every time either table changed for each user? | ||
-- Bonus: When was each of these changes valid? | ||
-- Double Bonus: How would you extend this to join onto a third table? Say the Claims table? | ||
-- @block | ||
SETTING DEFAULT VALID_TIME ALL | ||
SELECT Users.UserName, Policies.PolicyType--, Users._valid_time * Policies._valid_time | ||
FROM Users | ||
JOIN Policies | ||
ON Policies.UserId = Users._id | ||
WHERE Users._valid_time OVERLAPS Policies._valid_time | ||
ORDER BY Users._id | ||
|
||
-- Double bonus answer: | ||
-- @block | ||
SETTING DEFAULT VALID_TIME ALL | ||
SELECT Users.UserName, Policies.PolicyType--, Users._valid_time * Policies._valid_time * Claims._valid_time | ||
FROM Users | ||
JOIN Policies | ||
ON Policies.UserId = Users._id | ||
JOIN Claims | ||
ON Claims.PolicyId = Policies._id | ||
WHERE OVERLAPS(Users._valid_time, Policies._valid_time, Claims._valid_time) | ||
ORDER BY Users._id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- @conn xtdb | ||
|
||
-- Slide decks: | ||
-- - Introduction to Temporal SQL (SQL 2011) | ||
-- - Policy Management | ||
-- - Claims Management | ||
-- - High level benefits (synergy POC?) | ||
|
||
-- Talk about how we get data into the system | ||
|
||
-- - XTDB Features to talk about: | ||
-- - Schema-less | ||
-- - Temporal Joins | ||
-- - "Auto-versioning" | ||
|
||
-- Questions: | ||
-- - Should you include a "status" field? | ||
-- - Why are we "as of now" by default (non standard)? | ||
-- - | ||
|
||
-- Actions to explore: | ||
-- - Renewing a policy | ||
-- - Making a claim | ||
-- - Detecting fraud | ||
-- - Gap in cover (query find the gaps) | ||
-- - (_system_from - _valid_from) How far in the past was information entered? | ||
-- - Simple premium recalculation | ||
-- - Just based on points on license | ||
-- - Find out late someone got points | ||
-- - How much have they underpaid? | ||
-- - Sheduling a new policy (to avoid gaps) | ||
-- - Updating policy/claim details | ||
-- - When was each thing true (period intersection + overlaps) | ||
-- - No need for an audit log | ||
-- - Notifying policy holders of important date | ||
-- - time to renew, claim status changed recently | ||
-- - Offering discounts | ||
-- - Loyalty (Who has been an uninterupted customer for greater than n years?) | ||
-- - Compliance and regulatory reporting | ||
-- - Upselling (Given `Home`, `Life`, and `Health` who is missing one or more?) |