diff --git a/calculate_largest_expensors.sql b/calculate_largest_expensors.sql index e69de29..1766b27 100644 --- a/calculate_largest_expensors.sql +++ b/calculate_largest_expensors.sql @@ -0,0 +1,20 @@ +SELECT + e.employee_id, + CONCAT(e.first_name, ' ', e.last_name) AS employee_name, + e.manager_id, + CONCAT(m.first_name, ' ', m.last_name) AS manager_name, + SUM(x.unit_price * x.quantity) AS total_expensed_amount +FROM memory.default.sexi_expense x +JOIN memory.default.employee e + ON x.employee_id = e.employee_id +LEFT JOIN memory.default.employee m + ON e.manager_id = m.employee_id +GROUP BY + e.employee_id, + e.first_name, + e.last_name, + e.manager_id, + m.first_name, + m.last_name +HAVING SUM(x.unit_price * x.quantity) > 1000 +ORDER BY total_expensed_amount DESC; diff --git a/create_employees.sql b/create_employees.sql index e69de29..def8bd5 100644 --- a/create_employees.sql +++ b/create_employees.sql @@ -0,0 +1,20 @@ + +CREATE TABLE memory.default.employee ( + employee_id TINYINT, + first_name VARCHAR(255), + last_name VARCHAR(255), + job_title VARCHAR(255), + manager_id TINYINT +); + +INSERT INTO memory.default.employee (employee_id, first_name, last_name, job_title, manager_id) VALUES +(1, 'Ian', 'James', 'CEO', 4), +(2, 'Umberto', 'Torrielli', 'CSO', 1), +(3, 'Alex', 'Jacobson', 'MD EMEA', 2), +(4, 'Darren', 'Poynton', 'CFO', 2), +(5, 'Tim', 'Beard', 'MD APAC', 2), +(6, 'Gemma', 'Dodd', 'COS', 1), +(7, 'Lisa', 'Platten', 'CHR', 6), +(8, 'Stefano', 'Camisaca', 'GM Activation', 2), +(9, 'Andrea', 'Ghibaudi', 'MD NAM', 2); + diff --git a/create_expenses.sql b/create_expenses.sql index e69de29..99850d1 100644 --- a/create_expenses.sql +++ b/create_expenses.sql @@ -0,0 +1,21 @@ +CREATE TABLE memory.default.sexi_expense ( + employee_id TINYINT, + unit_price DECIMAL(8, 2), + quantity TINYINT +); + +INSERT INTO memory.default.sexi_expense (employee_id, unit_price, quantity) +SELECT e.employee_id, v.unit_price, v.quantity +FROM ( + VALUES + ('Alex','Jacobson', 6.50, 14), + ('Alex','Jacobson', 11.00, 20), + ('Alex','Jacobson', 22.00, 18), + ('Alex','Jacobson', 13.00, 75), + ('Andrea','Ghibaudi',300.00, 1), + ('Darren','Poynton', 40.00, 9), + ('Umberto','Torrielli',17.50, 4) +) AS v(first_name, last_name, unit_price, quantity) +JOIN memory.default.employee e + ON e.first_name = v.first_name + AND e.last_name = v.last_name; diff --git a/create_invoices.sql b/create_invoices.sql index e69de29..87ef8f7 100644 --- a/create_invoices.sql +++ b/create_invoices.sql @@ -0,0 +1,27 @@ +CREATE TABLE memory.default.sexi_invoice ( + supplier_id TINYINT, + invoice_amount DECIMAL(8, 2), + due_date DATE +); + +CREATE TABLE memory.default.sexi_supplier ( + supplier_id TINYINT, + name VARCHAR +); + +INSERT INTO memory.default.sexi_invoice (supplier_id, invoice_amount, due_date) VALUES +(5, 6000.00, DATE '2026-01-31'), +(1, 2000.00, DATE '2025-12-31'), +(1, 1500.00, DATE '2026-01-31'), +(2, 500.00, DATE '2025-11-30'), +(3, 6000.00, DATE '2026-01-31'), +(4, 4000.00, DATE '2026-04-30'); + + + +INSERT INTO memory.default.sexi_supplier (supplier_id, name) VALUES +(1, 'Catering Plus'), +(2, 'Dave''s Discos'), +(3, 'Entertainment tonight'), +(4, 'Ice Ice Baby'), +(5, 'Party Animals'); diff --git a/find_manager_cycles.sql b/find_manager_cycles.sql index e69de29..0928431 100644 --- a/find_manager_cycles.sql +++ b/find_manager_cycles.sql @@ -0,0 +1,24 @@ +WITH RECURSIVE manager_path(employee_id, manager_id, path) AS ( + SELECT + employee_id, + manager_id, + ARRAY[employee_id] AS path + FROM memory.default.employee + + UNION ALL + + SELECT + e.employee_id, + m.manager_id, + path || m.manager_id + FROM manager_path e + JOIN memory.default.employee m + ON e.manager_id = m.employee_id + WHERE m.manager_id IS NOT NULL + AND NOT contains(e.path, m.manager_id) +) +SELECT + employee_id, + path +FROM manager_path +WHERE contains(path, manager_id); diff --git a/generate_supplier_payment_plans.sql b/generate_supplier_payment_plans.sql index e69de29..f8df173 100644 --- a/generate_supplier_payment_plans.sql +++ b/generate_supplier_payment_plans.sql @@ -0,0 +1,46 @@ +WITH months AS ( + SELECT date_trunc('month', date_add('month', seq, current_date)) + INTERVAL '1' MONTH - INTERVAL '1' day AS payment_date + FROM UNNEST(sequence(0, date_diff('month', date_trunc('month', current_date), date_trunc('month', (SELECT max(due_date) FROM memory.default.sexi_invoice))))) AS t(seq) +), +invoice_payments AS ( + SELECT + i.supplier_id, + i.invoice_amount, + i.due_date, + months.payment_date, + date_trunc('month', current_date) + INTERVAL '1' MONTH - INTERVAL '1' day AS first_payment_date, + date_diff('month', date_trunc('month', current_date) + INTERVAL '1' MONTH - INTERVAL '1' day, i.due_date) + 1 AS total_months, + CASE + WHEN months.payment_date <= i.due_date AND months.payment_date >= (date_trunc('month', current_date) + INTERVAL '1' MONTH - INTERVAL '1' day) + THEN i.invoice_amount / (date_diff('month', date_trunc('month', current_date) + INTERVAL '1' MONTH - INTERVAL '1' day, i.due_date) + 1) + ELSE 0 + END AS monthly_payment + FROM memory.default.sexi_invoice i + CROSS JOIN months +), +supplier_monthly_payments AS ( + SELECT + supplier_id, + payment_date, + SUM(monthly_payment) AS payment_amount + FROM invoice_payments + WHERE monthly_payment > 0 + GROUP BY supplier_id, payment_date +), +supplier_balances AS ( + SELECT supplier_id, SUM(invoice_amount) AS balance_outstanding + FROM memory.default.sexi_invoice + GROUP BY supplier_id +) + +SELECT + s.supplier_id, + sp.name AS supplier_name, + smp.payment_amount, + sb.balance_outstanding, + smp.payment_date +FROM supplier_monthly_payments smp +JOIN supplier_balances sb ON smp.supplier_id = sb.supplier_id +JOIN memory.default.sexi_supplier sp ON sp.supplier_id = smp.supplier_id +JOIN (SELECT DISTINCT supplier_id FROM memory.default.sexi_invoice) s ON s.supplier_id = smp.supplier_id +ORDER BY s.supplier_id, smp.payment_date;