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
20 changes: 20 additions & 0 deletions calculate_largest_expensors.sql
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions create_employees.sql
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions create_expenses.sql
Original file line number Diff line number Diff line change
@@ -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);
40 changes: 40 additions & 0 deletions create_invoices.sql
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 24 additions & 0 deletions find_manager_cycles.sql
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 23 additions & 0 deletions generate_supplier_payment_plans.sql
Original file line number Diff line number Diff line change
@@ -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;