Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions calculate_largest_expensors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- =============================================================================
-- Task 5: Calculate the largest expensors (> 1000 threshold)
-- =============================================================================
-- Reports employees whose total expensed amount exceeds 1000, along with
-- their manager's details. Ordered by total_expensed_amount descending.
--
-- Expense totals in the current dataset:
-- Alex Jacobson (3): 6.50*14 + 11.00*20 + 22.00*18 + 13.00*75 = 1682.00
-- Darren Poynton (4): 40.00 * 9 = 360.00
-- Andrea Ghibaudi (9): 300.00 * 1 = 300.00
-- Umberto Torrielli (2): 17.50 * 4 = 70.00
--
-- Only Alex Jacobson exceeds the 1000 threshold.
-- =============================================================================

USE memory.default;

SELECT
emp.employee_id,
CONCAT(emp.first_name, ' ', emp.last_name) AS employee_name,
emp.manager_id,
CONCAT(mgr.first_name, ' ', mgr.last_name) AS manager_name,
expense_totals.total_expensed_amount
FROM (
-- Aggregate each employee's expenses: unit_price * quantity per line item
SELECT
ex.employee_id,
SUM(ex.unit_price * ex.quantity) AS total_expensed_amount
FROM EXPENSE ex
GROUP BY ex.employee_id
HAVING SUM(ex.unit_price * ex.quantity) > 1000
) expense_totals
JOIN EMPLOYEE emp ON expense_totals.employee_id = emp.employee_id
JOIN EMPLOYEE mgr ON emp.manager_id = mgr.employee_id
ORDER BY expense_totals.total_expensed_amount DESC;
33 changes: 33 additions & 0 deletions create_employees.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- =============================================================================
-- Task 1: Create the EMPLOYEE table
-- =============================================================================
-- Loads employee data from hr/employee_index.csv into the SExI database.
-- employee_id and manager_id are typed as TINYINT per the specification.
-- All other columns retain their natural VARCHAR type.
--
-- DROP TABLE IF EXISTS ensures this script is idempotent and can be re-run
-- without needing to restart the Trino container.
-- =============================================================================

USE memory.default;

DROP TABLE IF EXISTS EMPLOYEE;

CREATE TABLE EMPLOYEE (
employee_id TINYINT,
first_name VARCHAR,
last_name VARCHAR,
job_title VARCHAR,
manager_id TINYINT
);

INSERT INTO EMPLOYEE VALUES
(CAST(1 AS TINYINT), 'Ian', 'James', 'CEO', CAST(4 AS TINYINT)),
(CAST(2 AS TINYINT), 'Umberto', 'Torrielli', 'CSO', CAST(1 AS TINYINT)),
(CAST(3 AS TINYINT), 'Alex', 'Jacobson', 'MD EMEA', CAST(2 AS TINYINT)),
(CAST(4 AS TINYINT), 'Darren', 'Poynton', 'CFO', CAST(2 AS TINYINT)),
(CAST(5 AS TINYINT), 'Tim', 'Beard', 'MD APAC', CAST(2 AS TINYINT)),
(CAST(6 AS TINYINT), 'Gemma', 'Dodd', 'COS', CAST(1 AS TINYINT)),
(CAST(7 AS TINYINT), 'Lisa', 'Platten', 'CHR', CAST(6 AS TINYINT)),
(CAST(8 AS TINYINT), 'Stefano', 'Camisaca', 'GM Activation', CAST(2 AS TINYINT)),
(CAST(9 AS TINYINT), 'Andrea', 'Ghibaudi', 'MD NAM', CAST(2 AS TINYINT));
41 changes: 41 additions & 0 deletions create_expenses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- =============================================================================
-- Task 2: Create the EXPENSE table
-- =============================================================================
-- Loads expense receipts from finance/receipts_from_last_night/ into SExI.
-- Employee names are resolved to their employee_id from the EMPLOYEE table.
--
-- Source receipts and their mappings:
-- drinkies.txt -> Alex Jacobson (employee_id = 3) 6.50 x 14
-- drinks.txt -> Alex Jacobson (employee_id = 3) 11.00 x 20
-- drinkss.txt -> Alex Jacobson (employee_id = 3) 22.00 x 18
-- duh_i_think_i_got_.. -> Alex Jacobson (employee_id = 3) 13.00 x 75
-- i_got_lost_on_the_.. -> Andrea Ghibaudi (employee_id = 9) 300.00 x 1
-- ubers.txt -> Darren Poynton (employee_id = 4) 40.00 x 9
-- we_stopped_for_a_.. -> Umberto Torrielli (employee_id = 2) 17.50 x 4
-- =============================================================================

USE memory.default;

DROP TABLE IF EXISTS EXPENSE;

CREATE TABLE EXPENSE (
employee_id TINYINT,
unit_price DECIMAL(8, 2),
quantity TINYINT
);

INSERT INTO EXPENSE VALUES
-- drinkies.txt: Alex Jacobson - "Drinks, lots of drinks"
(CAST(3 AS TINYINT), CAST(6.50 AS DECIMAL(8,2)), CAST(14 AS TINYINT)),
-- drinks.txt: Alex Jacobson - "More Drinks"
(CAST(3 AS TINYINT), CAST(11.00 AS DECIMAL(8,2)), CAST(20 AS TINYINT)),
-- drinkss.txt: Alex Jacobson - "So Many Drinks!"
(CAST(3 AS TINYINT), CAST(22.00 AS DECIMAL(8,2)), CAST(18 AS TINYINT)),
-- duh_i_think_i_got_too_many.txt: Alex Jacobson - "I bought everyone in the bar a drink!"
(CAST(3 AS TINYINT), CAST(13.00 AS DECIMAL(8,2)), CAST(75 AS TINYINT)),
-- i_got_lost_on_the_way_home_and_now_im_in_mexico.txt: Andrea Ghibaudi
(CAST(9 AS TINYINT), CAST(300.00 AS DECIMAL(8,2)), CAST(1 AS TINYINT)),
-- ubers.txt: Darren Poynton - "Ubers to get us all home"
(CAST(4 AS TINYINT), CAST(40.00 AS DECIMAL(8,2)), CAST(9 AS TINYINT)),
-- we_stopped_for_a_kebabs.txt: Umberto Torrielli
(CAST(2 AS TINYINT), CAST(17.50 AS DECIMAL(8,2)), CAST(4 AS TINYINT));
65 changes: 65 additions & 0 deletions create_invoices.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- =============================================================================
-- Task 3: Create the SUPPLIER and INVOICE tables
-- =============================================================================
-- Loads supplier invoices from finance/invoices_due/ into SExI.
--
-- SUPPLIER table: supplier_id assigned alphabetically by company name:
-- 1 = Catering Plus
-- 2 = Dave's Discos
-- 3 = Entertainment tonight
-- 4 = Ice Ice Baby
-- 5 = Party Animals
--
-- INVOICE table: due_date is set to the last day of the month, calculated
-- relative to the current date using the "N months from now" specification
-- from each invoice file.
--
-- Note: The column name "invoice_ammount" preserves the spelling from the
-- task specification (intentional typo retained for compatibility).
-- =============================================================================

USE memory.default;

-- Drop in dependency order (INVOICE references SUPPLIER)
DROP TABLE IF EXISTS INVOICE;
DROP TABLE IF EXISTS SUPPLIER;

-- Create the SUPPLIER reference table (IDs assigned alphabetically)
CREATE TABLE SUPPLIER (
supplier_id TINYINT,
name VARCHAR
);

INSERT INTO SUPPLIER VALUES
(CAST(1 AS TINYINT), 'Catering Plus'),
(CAST(2 AS TINYINT), 'Dave''s Discos'),
(CAST(3 AS TINYINT), 'Entertainment tonight'),
(CAST(4 AS TINYINT), 'Ice Ice Baby'),
(CAST(5 AS TINYINT), 'Party Animals');

-- Create the INVOICE table with due dates as last day of the target month
CREATE TABLE INVOICE (
supplier_id TINYINT,
invoice_ammount DECIMAL(8, 2),
due_date DATE
);

INSERT INTO INVOICE VALUES
-- brilliant_bottles.txt: Catering Plus - Champagne, Whiskey, Vodka, etc. (2 months)
(CAST(1 AS TINYINT), CAST(2000.00 AS DECIMAL(8,2)),
last_day_of_month(date_add('month', 2, current_date))),
-- crazy_catering.txt: Catering Plus - Pizzas, Burgers, Hotdogs, etc. (3 months)
(CAST(1 AS TINYINT), CAST(1500.00 AS DECIMAL(8,2)),
last_day_of_month(date_add('month', 3, current_date))),
-- disco_dj.txt: Dave's Discos - Dave, Dave Equipment (1 month)
(CAST(2 AS TINYINT), CAST(500.00 AS DECIMAL(8,2)),
last_day_of_month(date_add('month', 1, current_date))),
-- excellent_entertainment.txt: Entertainment tonight (3 months)
(CAST(3 AS TINYINT), CAST(6000.00 AS DECIMAL(8,2)),
last_day_of_month(date_add('month', 3, current_date))),
-- fantastic_ice_sculptures.txt: Ice Ice Baby - Ice Luge, sculpture (6 months)
(CAST(4 AS TINYINT), CAST(4000.00 AS DECIMAL(8,2)),
last_day_of_month(date_add('month', 6, current_date))),
-- awesome_animals.txt: Party Animals - Zebra, Lion, Giraffe, Hippo (3 months)
(CAST(5 AS TINYINT), CAST(6000.00 AS DECIMAL(8,2)),
last_day_of_month(date_add('month', 3, current_date)));
Loading