Learning machine learning? Try my machine learning flashcards or Machine Learning with Python Cookbook.
Calculate Time Duration
Create Table
-- Create table called dead_adventurers
CREATE TABLE dead_adventurers (
-- string variable
name varchar(255),
-- string variable
race varchar(255),
-- string variable
weapon varchar(255),
-- date variable
started_adventure date,
-- date variable
died date
)
Insert Rows
-- Insert into the table dead_adventurers
INSERT INTO dead_adventurers (name, race, weapon, started_adventure, died)
VALUES ('Fjoak Doom-Wife', 'Human', 'Axe', '09-JAN-2017', '10-Nov-2017'),
('Alooneric Cortte', 'Elf', 'Bow', '10-JAN-2017', '11-JAN-2017'),
('Piperel Ramsay', 'Elf', 'Sword', '11-JAN-2017', '12-APR-2017'),
('Casimir Yardley', 'Elf', 'Magic', '23-JAN-2017', '06-MAY-2017')
Calculate Duration Between Two Date Values
-- Get all the columns, and add a new column called days_on_adventure
-- that is the number of days between the start of the adventurer and when they died
SELECT *, died - started_adventure AS days_on_adventure FROM dead_adventurers
name | race | weapon | started_adventure | died | days_on_adventure |
---|---|---|---|---|---|
Fjoak Doom-Wife | Human | Axe | 2017-01-09 | 2017-11-10 | 305 |
Alooneric Cortte | Elf | Bow | 2017-01-10 | 2017-01-11 | 1 |
Piperel Ramsay | Elf | Sword | 2017-01-11 | 2017-04-12 | 91 |
Casimir Yardley | Elf | Magic | 2017-01-23 | 2017-05-06 | 103 |