Counting the number of unique elements

Hi,

I have the following table:
CREATE TABLE test (
id UUID NOT NULL DEFAULT gen_random_uuid(),
nunique INT AS (json_array_length(arr)) STORED,
arr JSONB
);
insert into test (arr) values(’[10, 200, 200, 300, 400]’);

I would like to achieve two things:

  1. Only store unique elements in arr
  2. (this will be easy after 1:) nunique should contain the number of unique array elements

Is it possible to do with CRDB?

Thanks

Hi,

You can try:

insert into t select to_json(array (select distinct json_array_elements(’[10, 200, 200, 300, 400]’)::text::int));