Skip to content

Commit

Permalink
feat: add valid time exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Akeboshiwind committed Sep 29, 2024
1 parent 4fc46bd commit b494d5d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
41 changes: 41 additions & 0 deletions sql/04-valid-time.answers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- @conn xtdb

-- 👇 Run this first
-- @block
INSERT INTO users (_id, address, phone_number, email) VALUES
('John Doe', '123 Evergreen Row', '555-1234', '[email protected]'),
('Fiona Green', '456 Maple Street', '555-5678', '[email protected]'),
('Alice Johnson', '789 Oak Avenue', '555-8765', '[email protected]'),
('Bob Brown', '101 Pine Road', '555-4321', '[email protected]'),
('Evan Foster', '202 Birch Lane', '555-6789', '[email protected]'),
('George Harris', '303 Cedar Drive', '555-9876', '[email protected]');

-- Q: Fiona has changed her phone number to '666-8683', update the table:
-- @block
UPDATE users
SET phone_number = '666-8683'
WHERE _id = 'Fiona Green';

-- Q: Write a query to show how her phone number has changed over time and when in valid time
-- @block
SELECT phone_number, _valid_from
FROM users FOR VALID_TIME all
WHERE _id = 'Fiona Green';

-- Q: A user joined us a year ago, insert them into the database from when they joined in valid time with the details:
-- Name: 'Tony Felonius', email `[email protected]`, phone number `666-1111` and the address '404 Elm Street'
-- @block
INSERT users (_id, address, phone_number, email, _valid_from) VALUES
('Tony Felonius', '404 Elm Street', '666-1111', '[email protected]', TIMESTAMP '2023-09-29T00:00:00Z');

-- Q: Later they tell us that they were actually lived at the address '1234 Maple Lane' since 3 months ago, update the database
-- @block
UPDATE users FOR VALID_TIME FROM TIMESTAMP '2024-03-29T00:00:00Z'
SET address = '1234 Maple Lane'
WHERE _id = 'Tony Felonius';

-- Q: 'Alice Johnson' has told us that her address is going to change in 3 months to '5678 Oakwood Avenue', update the database in the future
-- @block
UPDATE users FOR VALID_TIME FROM TIMESTAMP '2024-12-29T00:00:00Z'
SET address = '5678 Oakwood Avenue'
WHERE _id = 'Alice Johnson';
32 changes: 32 additions & 0 deletions sql/04-valid-time.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- @conn xtdb

-- 👇 Run this first
-- @block
INSERT INTO users (_id, address, phone_number, email) VALUES
('John Doe', '123 Evergreen Row', '555-1234', '[email protected]'),
('Fiona Green', '456 Maple Street', '555-5678', '[email protected]'),
('Alice Johnson', '789 Oak Avenue', '555-8765', '[email protected]'),
('Bob Brown', '101 Pine Road', '555-4321', '[email protected]'),
('Evan Foster', '202 Birch Lane', '555-6789', '[email protected]'),
('George Harris', '303 Cedar Drive', '555-9876', '[email protected]');

-- Q: Fiona has changed her phone number to '666-8683', update the table:
-- @block
UPDATE ...;

-- Q: Write a query to show how her phone number has changed over time and when in valid time
-- @block
SELECT ...;

-- Q: A user joined us a year ago, insert them into the database from when they joined in valid time with the details:
-- Name: 'Tony Felonius', email `[email protected]`, phone number `666-1111` and the address '404 Elm Street'
-- @block
INSERT ...;

-- Q: Later they tell us that they were actually lived at the address '1234 Maple Lane' since 3 months ago, update the database
-- @block
UPDATE ...;

-- Q: 'Alice Johnson' has told us that her address is going to change in 3 months to '5678 Oakwood Avenue', update the database in the future
-- @block
UPDATE ...;

0 comments on commit b494d5d

Please sign in to comment.