LeetCode 181. Employees Earning More Than Their Managers

Tutd Schema:

employee := relation{id Int, name Text, salary Integer, managerId Maybe Int}
                   {tuple{id int(1), name "Joe", salary 70000, managerId (Just int(3))},
                    tuple{id int(2), name "Henry", salary 80000, managerId (Just int(4))},
                    tuple{id int(3), name "Sam", salary 60000, managerId Nothing},
                    tuple{id int(4), name "Max", salary 90000, managerId Nothing}};
key employee_pkey {id} employee;

Thinking Process:

TutorialD (master/main): :importtutd "181__Employees_Earning_More_Than_Their_Manager.tutd"

-- get employees who has manager.

TutorialD (master/main): employee_ := (((employee where ^isJust(@managerId)):{managerId_ := fromMaybe(int(0), @managerId)}){all but managerId} rename {managerId_ as managerId})

-- get managers who doesn't have manager.

TutorialD (master/main): manager := ((employee where not ^isJust(@managerId)){all but managerId}) rename {name as managerName, salary as managerSala
ry, id as managerId}

-- use join on managerId to find the one-to-one relationship and compare salaries.

-- ^gt doesn't work for Int. And I wish @a > @b can work. It seems AtomExpr can express a lot if haskell function can be used here. What if I can add operators on a @a :: RelationAtomType? can it have more expressivity?


TutorialD (master/main): :showexpr (employee_ join manager) where ^gt(@salary, @managerSalary)
┌───────┬──────────────┬─────────────────┬──────────────────────┬──────────┬───────────────┐
│id::Int│managerId::Int│managerName::Text│managerSalary::Integer│name::Text│salary::Integer│
├───────┼──────────────┼─────────────────┼──────────────────────┼──────────┼───────────────┤
│1      │3             │"Sam"            │60000                 │"Joe"     │70000          │
└───────┴──────────────┴─────────────────┴──────────────────────┴──────────┴───────────────┘


And here is the answer! Cool!

留言

這個網誌中的熱門文章

LeetCode 184. Department Highest Salary