3.5 有序 COUNT DISTINCT

SQL

SELECT count (DISTINCT CustomerID) iNum,count(1) Num 
FROM Orders
WHERE OrderDate>='2021-01-01' and OrderDate<='2021-12-31'

SPL

A
1 >st=date(“2021-01-01”), et=date(“2021-12-31”), start=days@o(st), end=days@o(et)
2 =file(“Orders_Account.ctx”).open().cursor@mx(CustomerID;OrderDate>=start && OrderDate <=end;2)
3 =A2.groups(;count(1):Num, icount@o(CustomerID):iNum)

A2 数据准备时使用过@p ,在这里只要简单使用@m选项就可以实现并行计算了
A3 icount@o()选项表示执行有序去重,不必在内存中保留中间缓存,只需要和上一条记录对比是否相同即可。常规的 icount() 则默认去重字段是无序的,必须在内存中保留中间缓存,当缓存超出内存容量时就会内存溢出


SQL

SELECT count(DISTINCT CustomerID),month(OrderDate) months
FROM Orders
WHERE OrderDate>='2021-01-01' and OrderDate<='2021-12-31'
GROUP BY month(OrderDate)

SPL

A
1 >st=date(“2021-01-01”), et=date(“2021-12-31”), start=days@o(st), end=days@o(et)
2 =file(“Orders_Account.ctx”).open().cursor@mx(CustomerID, OrderDate;OrderDate>=start && OrderDate <=end;2)
3 =A2.groups(month(OrderDate):months; icount@o(CustomerID):iNum)

A3 可以直接利用 groups+icount 函数计算分组后的 count distinct,不必先 group 后再计算。