Skip to content

Commit

Permalink
Update cumulative-unique.md
Browse files Browse the repository at this point in the history
  • Loading branch information
UnamedRus committed Feb 10, 2024
1 parent f2f8a98 commit c197a36
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions content/en/altinity-kb-queries-and-syntax/cumulative-unique.md
Expand Up @@ -20,23 +20,14 @@ INSERT INTO events SELECT
FROM numbers(15);
```

## Using arrays
## Using window functions (starting from Clickhouse 21.3)

```sql
WITH
groupArray(_ts) AS ts_arr,
groupArray(state) AS state_arr
SELECT
arrayJoin(ts_arr) AS ts,
arrayReduce('uniqExactMerge', arrayFilter((x, y) -> (y <= ts), state_arr, ts_arr)) AS uniq
FROM
(
SELECT
toStartOfDay(ts) AS _ts,
uniqExactState(user_id) AS state
FROM events
GROUP BY _ts
)
toStartOfDay(ts) AS ts,
uniqExactMerge(uniqExactState(user_id)) OVER (ORDER BY ts ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS uniq
FROM events
GROUP BY ts
ORDER BY ts ASC

┌──────────────────ts─┬─uniq─┐
Expand All @@ -47,19 +38,17 @@ ORDER BY ts ASC
2021-05-03 00:00:007
└─────────────────────┴──────┘

WITH arrayJoin(range(toUInt32(_ts) AS int, least(int + toUInt32((3600 * 24) * 5), toUInt32(toDateTime('2021-05-04 00:00:00'))), 3600 * 24)) AS ts_expanded
SELECT
toDateTime(ts_expanded) AS ts,
uniqExactMerge(state) AS uniq
ts,
uniqExactMerge(state) OVER (ORDER BY ts ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS uniq
FROM
(
SELECT
toStartOfDay(ts) AS _ts,
toStartOfDay(ts) AS ts,
uniqExactState(user_id) AS state
FROM events
GROUP BY _ts
GROUP BY ts
)
GROUP BY ts
ORDER BY ts ASC

┌──────────────────ts─┬─uniq─┐
Expand All @@ -71,14 +60,23 @@ ORDER BY ts ASC
└─────────────────────┴──────┘
```

## Using window functions (starting from Clickhouse 21.3)
## Using arrays

```sql
WITH
groupArray(_ts) AS ts_arr,
groupArray(state) AS state_arr
SELECT
toStartOfDay(ts) AS ts,
uniqExactMerge(uniqExactState(user_id)) OVER (ORDER BY ts ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS uniq
FROM events
GROUP BY ts
arrayJoin(ts_arr) AS ts,
arrayReduce('uniqExactMerge', arrayFilter((x, y) -> (y <= ts), state_arr, ts_arr)) AS uniq
FROM
(
SELECT
toStartOfDay(ts) AS _ts,
uniqExactState(user_id) AS state
FROM events
GROUP BY _ts
)
ORDER BY ts ASC

┌──────────────────ts─┬─uniq─┐
Expand All @@ -89,17 +87,19 @@ ORDER BY ts ASC
2021-05-03 00:00:007
└─────────────────────┴──────┘

WITH arrayJoin(range(toUInt32(_ts) AS int, least(int + toUInt32((3600 * 24) * 5), toUInt32(toDateTime('2021-05-04 00:00:00'))), 3600 * 24)) AS ts_expanded
SELECT
ts,
uniqExactMerge(state) OVER (ORDER BY ts ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS uniq
toDateTime(ts_expanded) AS ts,
uniqExactMerge(state) AS uniq
FROM
(
SELECT
toStartOfDay(ts) AS ts,
toStartOfDay(ts) AS _ts,
uniqExactState(user_id) AS state
FROM events
GROUP BY ts
GROUP BY _ts
)
GROUP BY ts
ORDER BY ts ASC

┌──────────────────ts─┬─uniq─┐
Expand Down

0 comments on commit c197a36

Please sign in to comment.