diff --git a/calculate_largest_expensors.sql b/calculate_largest_expensors.sql index e69de29..19f7c5c 100644 --- a/calculate_largest_expensors.sql +++ b/calculate_largest_expensors.sql @@ -0,0 +1,20 @@ +USE memory.default; + +WITH largest_expensors AS ( + SELECT + employee_id, + SUM(unit_price * quantity) AS total_expensed_amount + FROM expense + GROUP BY employee_id +) +SELECT + e.employee_id, + CONCAT(first_name, ' ', last_name) AS employee_name, + e.manager_id, + (SELECT CONCAT(first_name, ' ', last_name) FROM employee WHERE employee_id = e.manager_id) AS manager_name, + total_expensed_amount +FROM largest_expensors le +JOIN employee e + ON e.employee_id = le.employee_id +WHERE total_expensed_amount > 1000 +ORDER BY total_expensed_amount DESC; \ No newline at end of file diff --git a/create_employees.sql b/create_employees.sql index e69de29..96fd848 100644 --- a/create_employees.sql +++ b/create_employees.sql @@ -0,0 +1,21 @@ +USE memory.default; + +CREATE TABLE EMPLOYEE ( + employee_id TINYINT, + first_name VARCHAR, + last_name VARCHAR, + job_title VARCHAR, + manager_id TINYINT +); + +INSERT INTO EMPLOYEE + 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); \ No newline at end of file diff --git a/create_expenses.sql b/create_expenses.sql index e69de29..f84bafc 100644 --- a/create_expenses.sql +++ b/create_expenses.sql @@ -0,0 +1,17 @@ +USE memory.default; + +CREATE TABLE EXPENSE ( + employee_id TINYINT, + unit_price DECIMAL(8, 2), + quantity TINYINT +); + +INSERT INTO EXPENSE + VALUES + (3, 6.5, 14), + (3, 20, 11), + (3, 22, 18), + (3, 13, 75), + (9, 300, 1), + (4, 40, 9), + (2, 17.5, 4); \ No newline at end of file diff --git a/create_invoices.sql b/create_invoices.sql index e69de29..8eb0fa0 100644 --- a/create_invoices.sql +++ b/create_invoices.sql @@ -0,0 +1,40 @@ +USE memory.default; + +CREATE TABLE INVOICE ( + supplier_id TINYINT, + invoice_ammount DECIMAL(8, 2), + due_date DATE +); + +CREATE TABLE SUPPLIER ( + supplier_id TINYINT, + name VARCHAR +); + +INSERT INTO SUPPLIER +SELECT + ROW_NUMBER() OVER (ORDER BY name) AS supplier_id, + name +FROM ( +VALUES + ('Party Animals'), + ('Catering Plus'), + ('Dave''s Discos'), + ('Entertainment tonight'), + ('Ice Ice Baby') +) AS t (name); + +INSERT INTO INVOICE +SELECT + supplier_id, + invoice_amount, + LAST_DAY_OF_MONTH(DATE_ADD('MONTH', months, CURRENT_DATE)) AS due_date +FROM ( +VALUES + (5, 6000, 3), + (1, 2000, 2), + (1, 1500, 3), + (2, 500, 1), + (3, 6000, 3), + (4, 4000, 6) + ) AS t (supplier_id, invoice_amount, months); \ No newline at end of file diff --git a/find_manager_cycles.sql b/find_manager_cycles.sql index e69de29..52f3a03 100644 --- a/find_manager_cycles.sql +++ b/find_manager_cycles.sql @@ -0,0 +1,24 @@ +USE memory.default; + +WITH RECURSIVE manager_cycle (employee_id, manager_id, path) AS ( + SELECT + employee_id, + manager_id, + ARRAY[employee_id] AS path + FROM employee + + UNION ALL + + SELECT + mp.employee_id, + e.manager_id, + mp.path || e.employee_id + FROM manager_cycle mp + JOIN employee e ON mp.manager_id = e.employee_id + WHERE NOT CONTAINS(mp.path, e.employee_id) +) +SELECT + employee_id, + path || employee_id AS cycle_path +FROM manager_cycle +WHERE manager_id = employee_id; \ No newline at end of file diff --git a/generate_supplier_payment_plans.sql b/generate_supplier_payment_plans.sql index e69de29..e2a0a69 100644 --- a/generate_supplier_payment_plans.sql +++ b/generate_supplier_payment_plans.sql @@ -0,0 +1,23 @@ +USE memory.default; + +WITH months_cte AS ( + SELECT + LAST_DAY_OF_MONTH(DATE_ADD('month', n, DATE_TRUNC('month', DATE_ADD('month', 1, CURRENT_DATE)))) AS month + FROM UNNEST(SEQUENCE(0, 12)) AS t(n) +), +payment_plan AS ( + SELECT + s.name AS supplier_name, + m.month, + ROUND(i.invoice_ammount / (DATE_DIFF('month', DATE_TRUNC('month', LAST_DAY_OF_MONTH(DATE_ADD('month', 1, CURRENT_DATE))), DATE_TRUNC('month', i.due_date)) + 1), 2) AS monthly_payment + FROM invoice i + JOIN supplier s ON i.supplier_id = s.supplier_id + JOIN months_cte m ON m.month BETWEEN LAST_DAY_OF_MONTH(DATE_ADD('month', 1, CURRENT_DATE)) AND i.due_date +) +SELECT + supplier_name, + month AS payment_date, + SUM(monthly_payment) +FROM payment_plan +GROUP BY supplier_name, month +ORDER BY supplier_name, payment_date; \ No newline at end of file