2.4 前 N 名 / 后 N 名

查询今年每个月金额最大的三个订单的订单额
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) 表示取订单金额最大的三个订单额,这里参数间用逗号分隔,表示取值,如果分号分隔则取整条记录(如下例)