3.8 Complicated judgment on each group

 

One example is to count customers who place an order for at least three days consecutively in each month.
SQL

WITH a AS(
    SELECT count (1) AS num, day (OrderDate) days,
        CustomerID,year(OrderDate) Years,month(OrderDate) Months
    FROM Orders
    WHERE OrderDate>='2022-01-01'
    GROUP BY CustomerID, day (OrderDate), year(OrderDate),month(OrderDate)), 
b AS (
    SELECT
        lag(days,1) OVER(PARTITION BY CustomerID, Years,Months ORDER BY days) pd, 
        days, CustomerID,Years,Months
    FROM a),
c AS (
    SELECT *,
        CASE WHEN days-pd=1 THEN 0 ELSE 1 END AS tmp 
    FROM b),
d AS (
    SELECT CustomerID,Years,Months,sum (tmp) OVER (
        PARTITION BY CustomerID,Years,Months ORDER BY days) NoOrderDays
    FROM c),
e AS (
    SELECT CustomerID, Years,Months,NoOrderDays,count (*) ContinuousDays 
    FROM d 
    GROUP BY CustomerID,Years,Months,NoOrderDays),
f AS (
    SELECT CustomerID,Years,Months,max (ContinuousDays) mCon 
    FROM e 
    GROUP BY CustomerID,Years,Months 
    HAVING max (ContinuousDays)>=3)
SELECT Years,Months,count (*) 
FROM f 
GROUP BY Years,Months

SPL

A
1 >st=date(“2022-01-01”),start=days@o(st)
2 =file(“Orders_Account.ctx”).open().cursor(CustomerID,OrderDate;OrderDate>=start)
3 =A2.group(CustomerID,month@y(OrderDate))
4 =A3.(~.select@1((a=case(OrderDate-OrderDate[-1],0:a,1:a+1;1))==3))
5 =A4.groups(year(OrderDate):Years,month(OrderDate):Months; count(1))

A3 Fetch order records of each customer in each month in order as a set.
A4 On each grouped subset, find whether there are customers who place an order for three days continuously.
A5 As A4 returns data that is already ordered by CustomerID, we just perform count to achieve the count distinct.