5027

One Round Trip, Top N

Performance
Medium
Pro
query
top-n
date-filter
sql-budget

One Round Trip, Top N

Logistics opens every review meeting with the same slide: the customers we shipped the most to this period. The code behind it walks every customer, then every shipment document of that customer, then every line of that document — the DBA's trace shows a wall of tiny SELECT statements growing with the customer list. The ranking is right; the report is just having O(customers × documents) little conversations with the database instead of one.

Your job: the same ranking, one conversation.

Requirements

Create a codeunit named "Top Shippers" with one public procedure:

procedure TopShippedCustomers(CustomerNoFilter: Text; FromDate: Date; ToDate: Date; TopCount: Integer): List of [Text]

Rules:

  1. Consider every Customer whose "No." matches CustomerNoFilter — an AL filter expression for a Code field. The tests use patterns like TRYAL-T1-*; the filter is never empty.
  2. A customer's shipped volume comes from the Sales Shipment Line records of their shipment documents: a Sales Shipment Header belongs to the customer through its "Sell-to Customer No.", and a line belongs to its document through "Document No.".
  3. Only lines of documents whose header "Posting Date" lies in FromDate..ToDate count — both boundary dates included. Shipment lines carry a "Posting Date" and a "Sell-to Customer No." of their own — stamps copied at posting time, possibly stale. The tests deliberately seed lines whose stamps contradict their header; the header decides.
The rest of the statement and 3 hints Unlock with Pro
ALBusiness Central 28.5
Pro
Pro problem

Solve it with Pro

You’re reading the opening of the statement. Pro unlocks the whole problem:

  • The full statement
  • Starter code in the browser editor
  • 13 grading tests, run on Business Central 28.5
  • 3 hints
  • The reference solution

Pro also opens all 92 Pro problems and lifts the daily limits on compile checks and submissions.

$10 / month
or $100 / year
Upgrade
This problem is part of Pro
Pro unlocks the full statement, tests, hints and solution. The code editor works on a computer.
Upgrade