diff --git a/calculate_largest_expensors.sql b/calculate_largest_expensors.sql index e69de29..243318d 100644 --- a/calculate_largest_expensors.sql +++ b/calculate_largest_expensors.sql @@ -0,0 +1,12 @@ +select e.employee_id,e.first_name || ' ' || e.last_name as employee_id +,m.manager_id, +m.first_name || ' ' || m.last_name as manager_name + + from EMPLOYEE e +left join +(select employee_id,sum(unit_price*quantity) as total_expensed_amount from EXPENSE group by 1)ex +on e.employee_id=ex.employee_id +left join +EMPLOYEE m +on m.employee_id=e.manager_id +where total_expensed_amount>1000 \ No newline at end of file diff --git a/create_employees.sql b/create_employees.sql index e69de29..1b5a56f 100644 --- a/create_employees.sql +++ b/create_employees.sql @@ -0,0 +1,24 @@ +CREATE TABLE EMPLOYEE ( + employee_id tinyint, + first_name varchar, + last_name varchar, + job_title varchar, + manager_id tinyint +); + +INSERT INTO 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); \ No newline at end of file diff --git a/create_expenses.sql b/create_expenses.sql index e69de29..782c489 100644 --- a/create_expenses.sql +++ b/create_expenses.sql @@ -0,0 +1,23 @@ +CREATE TABLE temp_emp ( + Employee_name varchar, + price DECIMAL(8, 2), + qty tinyint +); + + +INSERT INTO temp_emp ( + Employee_name, + price, + qty +) VALUES +('Alex Jacobson', 6.50, 14), +('Alex Jacobson', 11.00, 20), +('Alex Jacobson', 2.00, 18), +('Alex Jacobson', 13.00, 75), +('Andrea Ghibaudi', 300, 1), +('Darren Poynton', 40.00, 9), +('Umberto Torrielli', 17.50, 4); + +create table EXPENSE as select e.employee_id,price as unit_price,qty as quantity from temp_emp ex left outer join +EMPLOYEE e on ex.Employee_name=e.first_name || ' ' || e.last_name; +drop table temp_emp; \ No newline at end of file diff --git a/create_invoices.sql b/create_invoices.sql index e69de29..7eec3d5 100644 --- a/create_invoices.sql +++ b/create_invoices.sql @@ -0,0 +1,30 @@ +create table INVOICE +(supplier_id TINYINT, + invoice_ammount DECIMAL(8, 2), + due_date DATE); + +create table SUPPLIER +(supplier_id TINYINT, +name VARCHAR); + +INSERT INTO INVOICE ( + supplier_id, + invoice_ammount, + due_date +) VALUES +(5, 6000, '2025-12-31'), +(1, 2000, '2025-11-30'), +(1, 1500, '2025-12-31'), +(2, 500, '2025-10-31'), +(3, 6000, '2025-12-31'), +(4, 4000, '2026-03-31'); + +INSERT INTO SUPPLIER ( + supplier_id, + name +) VALUES +(5, 'Party Animals'), +(1, 'Catering Plus'), +(2, 'Dave''s Discos'), +(3, 'Entertainment tonight'), +(4, 'Ice Ice Baby'); \ No newline at end of file diff --git a/find_manager_cycles.sql b/find_manager_cycles.sql index e69de29..80df929 100644 --- a/find_manager_cycles.sql +++ b/find_manager_cycles.sql @@ -0,0 +1 @@ +select employee_id,array_agg(manager_id) as in_loop from EMPLOYEE group by 1 \ No newline at end of file diff --git a/generate_supplier_payment_plans.sql b/generate_supplier_payment_plans.sql index e69de29..42280d8 100644 --- a/generate_supplier_payment_plans.sql +++ b/generate_supplier_payment_plans.sql @@ -0,0 +1,8 @@ +select +i.supplier_id,s.name as supplier_name, sum(invoice_ammount) as payment_amount, sum(invoice_ammount) over(partition by i.supplier_id) as balance_outstanding,due_date as payment_date + +from INVOICE i +left outer join +SUPPLIER s +on i.supplier_id=s.supplier_id +group by 1,2,due_date \ No newline at end of file