Clickhouse uniqExact Aggregate function not work correctly

2.6k Views Asked by At

I have table with this schema

event_time DateTime,
field1 String,
field2 String,
field3 String

I want to create new table(table) with SummingMergeTree engine to aggregate data for some online queries. For this I create table like this:

CREATE TABLE aggregated_table(
  event_day DateTime,
  field1 String,
  field2 String,
  record_num UInt64,
  uniq_field3_state AggregateFunction(uniqExact, String)
)
ENGINE = SummingMergeTree()
ORDER BY (event_day, field1, field2);

After that I insert my data to new table:

INSERT INTO aggregated_table
SELECT
  toStartOfDay(event_time) as event_day,
  field1,
  field2,
  count(*) AS request_num,
  uniqExactState(field3) As uniq_field3_state
FROM table
GROUP BY event_day, field1, field2;

Now, when I query to find uniqExact of field3:

select uniqExact(uniq_field3_state) from aggregated_table;

my answer is:

┌─uniqExact(uniq_field3_state)─┐
│                       356948 │
└──────────────────────────────┘

but when I query the source table my answer is:

┌─uniqExact(field3)─┐
│             15548 │
└───────────────────┘

Is there any problem with this workflow?

1

There are 1 best solutions below

0
On BEST ANSWER

You must finalize STATEs calculations by -Merge Or -Finalize functions.

In your example you have to use uniqExactMerge not uniqExact.

select uniqExactMerge(uniq_field3_state) 
from aggregated_table;