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
22 changes: 22 additions & 0 deletions calculate_largest_expensors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
USE memory.default;

WITH employee_expenses AS (
SELECT
employee_id,
SUM(unit_price * quantity) AS total_expensed_amount
FROM expense
GROUP BY employee_id
)
SELECT
employee.employee_id,
employee.first_name || ' ' || employee.last_name AS employee_name,
employee.manager_id,
manager.first_name || ' ' || manager.last_name AS manager_name,
employee_expenses.total_expensed_amount
FROM employee_expenses
INNER JOIN employee
ON employee.employee_id = employee_expenses.employee_id
INNER JOIN employee AS manager
ON manager.employee_id = employee.manager_id
WHERE employee_expenses.total_expensed_amount > 1000
ORDER BY total_expensed_amount DESC;
29 changes: 29 additions & 0 deletions create_employees.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
USE memory.default;

DROP TABLE IF EXISTS employee;

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);
23 changes: 23 additions & 0 deletions create_expenses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
USE memory.default;

DROP TABLE IF EXISTS expense;

CREATE TABLE expense (
employee_id TINYINT,
unit_price DECIMAL(8, 2),
quantity TINYINT
);

INSERT INTO expense (
employee_id,
unit_price,
quantity
)
VALUES
(3, 13.00, 75),
(3, 22.00, 18),
(4, 40.00, 9),
(9, 300.00, 1),
(2, 17.50, 4),
(3, 11.00, 20),
(3, 6.50, 14);
39 changes: 39 additions & 0 deletions create_invoices.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
USE memory.default;

DROP TABLE IF EXISTS invoice;
DROP TABLE IF EXISTS supplier;

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
(1, 1500.00, last_day_of_month(date_add('month', 3, current_date))),
(2, 500.00, last_day_of_month(date_add('month', 1, current_date))),
(4, 4000.00, last_day_of_month(date_add('month', 6, current_date))),
(1, 2000.00, last_day_of_month(date_add('month', 2, current_date))),
(3, 6000.00, last_day_of_month(date_add('month', 3, current_date))),
(5, 6000.00, last_day_of_month(date_add('month', 3, current_date)));
41 changes: 41 additions & 0 deletions find_manager_cycles.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
USE memory.default;

WITH RECURSIVE manager_chain (
root_employee_id,
current_employee_id,
manager_id,
path,
found_cycle
) AS (
SELECT
employee_id,
employee_id,
manager_id,
ARRAY[employee_id],
false
FROM employee

UNION ALL

SELECT
manager_chain.root_employee_id,
employee.employee_id,
employee.manager_id,
manager_chain.path || employee.employee_id,
employee.employee_id = manager_chain.root_employee_id
FROM manager_chain
INNER JOIN employee
ON employee.employee_id = manager_chain.manager_id
WHERE
NOT manager_chain.found_cycle
AND (
NOT contains(manager_chain.path, employee.employee_id)
OR employee.employee_id = manager_chain.root_employee_id
)
)
SELECT
root_employee_id AS employee_id,
array_join(transform(path, x -> CAST(x AS VARCHAR)), ',') AS manager_loop
FROM manager_chain
WHERE found_cycle
ORDER BY employee_id;
63 changes: 63 additions & 0 deletions generate_supplier_payment_plans.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
USE memory.default;

WITH invoice_spreads AS (
SELECT
invoice.supplier_id,
CAST(invoice.invoice_ammount * 100 AS BIGINT) AS invoice_amount_cents,
CAST(
date_diff(
'month',
CAST(date_trunc('month', current_date) AS DATE),
CAST(date_trunc('month', invoice.due_date) AS DATE)
) AS INTEGER
) AS payment_month_count
FROM invoice
),
invoice_monthly_payments AS (
SELECT
invoice_spreads.supplier_id,
last_day_of_month(date_add('month', payment_month.month_number, current_date)) AS payment_date,
invoice_spreads.invoice_amount_cents / invoice_spreads.payment_month_count
+ CASE
WHEN payment_month.month_number < invoice_spreads.invoice_amount_cents % invoice_spreads.payment_month_count
THEN 1
ELSE 0
END AS payment_amount_cents
FROM invoice_spreads
CROSS JOIN UNNEST(sequence(0, payment_month_count - 1)) AS payment_month(month_number)
),
supplier_monthly_payments AS (
SELECT
supplier_id,
payment_date,
SUM(payment_amount_cents) AS payment_amount_cents
FROM invoice_monthly_payments
GROUP BY
supplier_id,
payment_date
),
supplier_totals AS (
SELECT
supplier_id,
SUM(CAST(invoice_ammount * 100 AS BIGINT)) AS total_invoice_amount_cents
FROM invoice
GROUP BY supplier_id
)
SELECT
supplier_monthly_payments.supplier_id,
supplier.name AS supplier_name,
CAST(supplier_monthly_payments.payment_amount_cents AS DECIMAL(8, 2)) / 100.00 AS payment_amount,
CAST(supplier_totals.total_invoice_amount_cents
- SUM(supplier_monthly_payments.payment_amount_cents) OVER (
PARTITION BY supplier_monthly_payments.supplier_id
ORDER BY supplier_monthly_payments.payment_date
) AS DECIMAL(8, 2)) / 100.00 AS balance_outstanding,
supplier_monthly_payments.payment_date
FROM supplier_monthly_payments
INNER JOIN supplier
ON supplier.supplier_id = supplier_monthly_payments.supplier_id
INNER JOIN supplier_totals
ON supplier_totals.supplier_id = supplier_monthly_payments.supplier_id
ORDER BY
supplier_monthly_payments.supplier_id,
supplier_monthly_payments.payment_date;