How to resolve the algorithm Top rank per group step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Top rank per group step by step in the EchoLisp programming language

Table of Contents

Problem Statement

Find the top   N   salaries in each department,   where   N   is provided as a parameter. Use this data as a formatted internal data structure (adapt it to your language-native idioms, rather than parse at runtime), or identify your external data source:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Top rank per group step by step in the EchoLisp programming language

Source code in the echolisp programming language

(lib 'struct) ;; tables are based upon structures
(lib 'sql)  ;; sql-select function

;; input table
(define emps  (make-table (struct emp (name id salary dept))))
;; output table
(define high  (make-table (struct out (dept name salary))))

;; sort/group procedure
(define (get-high num-records: N into: high)
(sql-select emp.dept emp.name emp.salary 
  from emps  
  group-by emp.dept 
  order-by emp.salary desc limit N into high))


(define emps_file 
'(("Tyler Bennett" E10297 32000 D101)
("John Rappl" E21437 47000 D050)
("George Woltman" E00127 53500 D101)
("Adam Smith" E63535 18000 D202)
("Claire Buckman" E39876 27800 D202)
("David McClellan" E04242 41500 D101)
("Rich Holcomb" E01234 49500 D202)
("Simon Gallubert" E00000 42 D666)
("Nathan Adams" E41298 21900 D050)
("Richard Potter" E43128 15900 D101)
("David Motsinger" E27002 19250 D202)
("Tim Sampair" E03033 27000 D101)
("Kim Arlich" E10001 57000 D190)
("Timothy Grove" E16398 29900 D190)))

(list->table emps_file emps ) ;; load the table

(get-high 2 high)
(table-print high)

[0]   D050  John Rappl       47000 
[1]   D050  Nathan Adams     21900 
[2]   D101  George Woltman   53500 
[3]   D101  David McClellan  41500 
[4]   D190  Kim Arlich       57000 
[5]   D190  Timothy Grove    29900 
[6]   D202  Rich Holcomb     49500 
[7]   D202  Claire Buckman   27800 
[8]   D666  Simon Gallubert  42  

(sql-delete from high)
(get-high 1 high)
(table-print high)
[0]   D050  John Rappl       47000 
[1]   D101  George Woltman   53500 
[2]   D190  Kim Arlich       57000 
[3]   D202  Rich Holcomb     49500 
[4]   D666  Simon Gallubert  42


  

You may also check:How to resolve the algorithm Substitution cipher step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Q programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the R programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Julia programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the PureBasic programming language