The goal is to find an equivalent of DISTINCT inside window COUNT for a sliding/cumulative window. According to COUNT:
When this function is called as a window function with an OVER clause that contains an ORDER BY clause:
- Using the keyword DISTINCT inside the window function is prohibited and results in a compile-time error.
Sample:
SELECT *, COUNT(DISTINCT val) OVER(ORDER BY id)
FROM VALUES (0, NULL),(1,10),(2,20),(3,20),(4,30),(5, 20),(6, 10) AS s(id, val)
ORDER BY id;
Error: distinct cannot be used with a window frame or an order.
Expected output:
SELECT *,
COUNT(DISTINCT val) OVER(ORDER BY id) AS CNT_CUMULATIVE,
COUNT(DISTINCT val) OVER(ORDER BY id ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)
AS CNT_SLIDING
FROM VALUES (0, NULL),(1,10),(2,20),(3,20),(4,30),(5, 20),(6, 10) AS s(id, val)
ORDER BY id;
+----+-----+----------------+-------------+
| ID | VAL | CNT_CUMULATIVE | CNT_SLIDING |
+----+-----+----------------+-------------+
| 0 | | 0 | 0 |
| 1 | 10 | 1 | 1 |
| 2 | 20 | 2 | 2 |
| 3 | 20 | 2 | 2 |
| 4 | 30 | 3 | 3 |
| 5 | 20 | 3 | 2 |
| 6 | 10 | 3 | 3 |
+----+-----+----------------+-------------+


bitand_aggorbitmap_construct_aggsupport sliding windows.