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 @@
SELECT
e.employee_id,
e.first_name || ' ' || e.last_name AS employee_name,
e.manager_id,
m.first_name || ' ' || m.last_name AS manager_name,
SUM(x.unit_price * x.quantity) AS total_expensed_amount
FROM employee e
JOIN expense x
ON e.employee_id = x.employee_id
LEFT JOIN 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) > DECIMAL '1000.00'
ORDER BY total_expensed_amount DESC;
26 changes: 26 additions & 0 deletions create_employees.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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);

20 changes: 20 additions & 0 deletions create_expenses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE expense (
employee_id TINYINT,
unit_price DECIMAL(8, 2),
quantity TINYINT
);

INSERT INTO expense (
employee_id,
unit_price,
quantity
)
VALUES
(3, DECIMAL '6.50', 14),
(3, DECIMAL '11.00', 20),
(3, DECIMAL '22.00', 18),
(3, DECIMAL '13.00', 75),
(9, DECIMAL '300.00', 1),
(4, DECIMAL '40.00', 9),
(2, DECIMAL '17.50', 4);

35 changes: 35 additions & 0 deletions create_invoices.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE TABLE supplier (
supplier_id TINYINT,
name VARCHAR
);

CREATE TABLE invoice (
supplier_id TINYINT,
invoice_ammount DECIMAL(8, 2),
due_date DATE
);

INSERT INTO supplier (
supplier_id,
name
)
VALUES
(1, 'Catering Plus'),
(2, 'Dave''s Discos'),
(3, 'Entertainment tonight'),
(4, 'Ice Ice Baby'),
(5, 'Party Animals');

INSERT INTO invoice (
supplier_id,
invoice_ammount,
due_date
)
VALUES
(5, DECIMAL '6000.00', DATE '2026-09-30'),
(1, DECIMAL '2000.00', DATE '2026-08-31'),
(1, DECIMAL '1500.00', DATE '2026-09-30'),
(2, DECIMAL '500.00', DATE '2026-07-31'),
(3, DECIMAL '6000.00', DATE '2026-09-30'),
(4, DECIMAL '4000.00', DATE '2026-12-31');

35 changes: 35 additions & 0 deletions find_manager_cycles.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
WITH RECURSIVE manager_paths (
start_employee_id,
current_employee_id,
path,
cycle_found
) AS (
SELECT
employee_id AS start_employee_id,
manager_id AS current_employee_id,
ARRAY[employee_id, manager_id] AS path,
employee_id = manager_id AS cycle_found
FROM employee

UNION ALL

SELECT
mp.start_employee_id,
e.manager_id AS current_employee_id,
concat(mp.path, ARRAY[e.manager_id]) AS path,
contains(mp.path, e.manager_id) AS cycle_found
FROM manager_paths mp
JOIN employee e
ON mp.current_employee_id = e.employee_id
WHERE mp.cycle_found = false
)
SELECT DISTINCT
element_at(path, cardinality(path)) AS employee_id,
slice(
path,
array_position(path, element_at(path, cardinality(path))),
cardinality(path) - array_position(path, element_at(path, cardinality(path))) + 1
) AS loop
FROM manager_paths
WHERE cycle_found = true
ORDER BY employee_id;
69 changes: 69 additions & 0 deletions generate_supplier_payment_plans.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
WITH invoice_payment_months AS (
SELECT
invoice.supplier_id,
supplier.name AS supplier_name,
invoice.invoice_ammount,
invoice.due_date,
date_diff('month', DATE '2026-06-30', invoice.due_date) AS payment_month_count
FROM invoice
JOIN supplier
ON invoice.supplier_id = supplier.supplier_id
),
invoice_monthly_payments AS (
SELECT
supplier_id,
supplier_name,
invoice_ammount / payment_month_count AS payment_amount,
last_day_of_month(date_add('month', month_offset, DATE '2026-06-01')) AS payment_date
FROM invoice_payment_months
CROSS JOIN UNNEST(sequence(0, payment_month_count - 1)) AS months(month_offset)
),
supplier_monthly_payments AS (
SELECT
supplier_id,
supplier_name,
payment_date,
SUM(payment_amount) AS payment_amount
FROM invoice_monthly_payments
GROUP BY
supplier_id,
supplier_name,
payment_date
),
supplier_totals AS (
SELECT
supplier_id,
SUM(invoice_ammount) AS total_invoice_amount
FROM invoice
GROUP BY supplier_id
),
payment_plan AS (
SELECT
supplier_monthly_payments.supplier_id,
supplier_monthly_payments.supplier_name,
supplier_monthly_payments.payment_date,
supplier_monthly_payments.payment_amount,
supplier_totals.total_invoice_amount
FROM supplier_monthly_payments
JOIN supplier_totals
ON supplier_monthly_payments.supplier_id = supplier_totals.supplier_id
)
SELECT
supplier_id,
supplier_name,
CAST(payment_amount AS DECIMAL(8, 2)) AS payment_amount,
CAST(
total_invoice_amount
- SUM(payment_amount) OVER (
PARTITION BY supplier_id
ORDER BY payment_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
AS DECIMAL(8, 2)
) AS balance_outstanding,
payment_date
FROM payment_plan
ORDER BY
supplier_id,
payment_date;