2.4 Getting top/bottom N values
Find amounts of top three orders in terms of amount per month in the current year:
SQL
WITH m AS(
SELECT year(OrderDate) years, month(OrderDate) months, Amount,
rank() OVER(PARTITION BY year(OrderDate),month(OrderDate)
ORDER BY Amount DESC) r
FROM Orders
WHERE OrderDate>='2022-01-01')
SELECT *
FROM m
WHERE r<=3
SPL
A | |
---|---|
1 | >st=date(“2022-01-01”), start=days@o(st) |
2 | =file(“Orders.ctx”).open().cursor@x(OrderDate, Amount; OrderDate>=start) |
3 | =A2.groups(year(OrderDate):years,month(OrderDate):months;top(3,-Amount):Amount) |
A3 top(3,-Amount) gets Amount values of top three order records with the largest amounts. The comma-separated parameters enable getting values, while semicolon-separated ones get records (as in the next example).
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL