發表文章

目前顯示的是 4月, 2021的文章

262. Trips And Users

Tutd Schema: data Status = Completed | CancelledByDriver | CancelledByClient; trips := relation{id Integer, client_id Integer, driver_id Integer, city_id Integer, status Status, request_at Day}{   tuple{id 1, client_id 1, driver_id 10, city_id 1, status Completed, request_at fromGregorian(2013,10,1)},   tuple{id 2, client_id 2, driver_id 11, city_id 1, status CancelledByDriver, request_at fromGregorian(2013,10,1)},   tuple{id 3, client_id 3, driver_id 12, city_id 6, status Completed, request_at fromGregorian(2013,10,1)},   tuple{id 4, client_id 4, driver_id 13, city_id 6, status CancelledByClient, request_at fromGregorian(2013,10,1)},   tuple{id 5, client_id 1, driver_id 10, city_id 1, status Completed, request_at fromGregorian(2013,10,2)},   tuple{id 6, client_id 2, driver_id 11, city_id 6, status Completed, request_at fromGregorian(2013,10,2)},   tuple{id 7, client_id 3, driver_id 12, city_id 6, status Completed, request_at fromGregorian(2013,10,2)},   tuple{id 8, client_id 2, driver

178 Rank Scores

-- "dataframe to relation feature" is needed rank := :showdataframe scores{score} orderby{score descending} ┌──┬──────────────┐ │DF│score::Double⬇│ ├──┼──────────────┤ │1 │4.0           │ │2 │3.85          │ │3 │3.65          │ │4 │3.5           │ └──┴──────────────┘ :showexpr scores join rank ┌───────────┬───────────┬─────────────┐ │df::Integer│id::Integer│score::Double│ ├───────────┼───────────┼─────────────┤ │1          │5          │4.0          │ │3          │2          │3.65         │ │4          │1          │3.5          │ │3          │6          │3.65         │ │1          │3          │4.0          │ │2          │4          │3.85         │ └───────────┴───────────┴─────────────┘ :showdataframe (scores join rank) orderby {df ascending} ┌──┬────────────┬────────────┬──────────────┐ │DF│df::Integer⬆│id::Integer↕│score::Double↕│ ├──┼────────────┼────────────┼──────────────┤ │1 │1           │3           │4.0           │ │2 │1           │5           │4.0           │ │3 │2

1179. Reformat Department Table

Tutd Schema: data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec; department := relation{ id Integer, revenue Integer, month Month}{  tuple{id 1, revenue 8000,  month Jan},  tuple{id 2, revenue 9000,  month Jan},  tuple{id 3, revenue 10000,  month Feb},  tuple{id 1, revenue 7000,  month Feb},  tuple{id 1, revenue 6000,  month Mar}}; key department_ukey {id, month} department; Thinking Process: -- if I don't want to see Nothing/null all over around... department group ({all but month}) as departmentRevenue) -- if I need Nothing to make the statement more clear...I've found a way to do that on LeetCode 175. Combine Two Tables. It involves cross product, not matching, extend Nothings, add Justs. But maybe a case expression can simplify it.    -- :{ tmp := case @atom isIn set of true -> Just atom; false -> Nothing -- or an operation a _add_Just_if_they_can_join_and_add_Nothing_while_they_cannot_ b answer := (((department where month=Jan){a

185. Department Top Three Salaries

Tutd Schema: employee :: { id Integer, name Text, salary Integer, departmentId Integer}; employee := relation{  tuple{id 1, name "Joe", salary 85000, departmentId 1},  tuple{id 2, name "Henry", salary 80000, departmentId 2},  tuple{id 3, name "Sam", salary 60000, departmentId 2},  tuple{id 4, name "Max", salary 90000, departmentId 1},  tuple{id 5, name "Janet", salary 69000, departmentId 1},  tuple{id 6, name "Randy", salary 85000, departmentId 1},  tuple{id 7, name "Will", salary 70000, departmentId 1}}; key employee_ukey {id} employee; department := relation{id Integer, name Text}{tuple{id 1, name "IT"}, tuple{id 2, name "Sales"}}; key department_ukey {id} department; Thinking Process: -- DataFrame -> Relation is needed to bring top 3 back to a set -- "map" departmentId by hand topThreeDep1 := (:showdataframe ((employee where departmentId = 1){salary}) orderby {salary desce

177. Nth Highest Salary

Tutd Schema:  employee := relation{id Integer, salary Integer}{   tuple{id 1, salary 100},   tuple{id 2, salary 200},   tuple{id 3, salary 300}}; Thinking Process: -- nth highest salary :showdataframe employee{salary} orderby {salary descending} offset n-1 limit 1 -- Right now, we don't have function definition like "getNthHighestSalary n = employee{salary} orderby {salary descending} offset (n-1) limit 1"