> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# uniqArrayIf

> uniqArrayIf 조합자를 사용하는 예시

<div id="description">
  ## 설명
</div>

[`Array`](/ko/reference/functions/aggregate-functions/combinators#-array) 및 [`If`](/ko/reference/functions/aggregate-functions/combinators#-if) 조합자는 [`uniq`](/ko/reference/functions/aggregate-functions/uniq)
함수에 적용할 수 있으며, 집계 조합자 함수인 `uniqArrayIf`를 사용하면
조건이 true인 행의 배열에서 고유한 값의 수를 계산할 수 있습니다.

<Note>
  -`If`와 -`Array`는 함께 사용할 수 있습니다. 단, `Array`가 먼저 오고 그다음에 `If`가 와야 합니다.
</Note>

이 함수는 `arrayJoin`을 사용하지 않고도
특정 조건에 따라 배열의 고유 요소 수를 계산해야 할 때 유용합니다.

<div id="example-usage">
  ## 사용 예시
</div>

<div id="count-unique-products">
  ### 세그먼트 유형과 참여 수준별로 조회된 고유 제품 수 계산
</div>

이 예시에서는 사용자 쇼핑 세션 데이터가 저장된 테이블을 사용하여, 특정 사용자 세그먼트에 속하고
세션 체류 시간을 나타내는 참여 메트릭을 기준으로 사용자가 조회한
고유 제품 수를 계산합니다.

```sql title="Query" theme={null}
CREATE TABLE user_shopping_sessions
(
    session_date Date,
    user_segment String,
    viewed_products Array(String),
    session_duration_minutes Int32
) ENGINE = Memory;

INSERT INTO user_shopping_sessions VALUES
    ('2024-01-01', 'new_customer', ['smartphone_x', 'headphones_y', 'smartphone_x'], 12),
    ('2024-01-01', 'returning', ['laptop_z', 'smartphone_x', 'tablet_a'], 25),
    ('2024-01-01', 'new_customer', ['smartwatch_b', 'headphones_y', 'fitness_tracker'], 8),
    ('2024-01-02', 'returning', ['laptop_z', 'external_drive', 'laptop_z'], 30),
    ('2024-01-02', 'new_customer', ['tablet_a', 'keyboard_c', 'tablet_a'], 15),
    ('2024-01-02', 'premium', ['smartphone_x', 'smartwatch_b', 'headphones_y'], 22);

-- 세그먼트 유형 및 참여 수준별 고유 제품 조회 수 집계
SELECT 
    session_date,
    -- 신규 고객의 장시간 세션에서 조회된 고유 제품 수 집계
    uniqArrayIf(viewed_products, user_segment = 'new_customer' AND session_duration_minutes > 10) AS new_customer_engaged_products,
    -- 재방문 고객이 조회한 고유 제품 수 집계
    uniqArrayIf(viewed_products, user_segment = 'returning') AS returning_customer_products,
    -- 전체 세션에서 조회된 고유 제품 수 집계
    uniqArray(viewed_products) AS total_unique_products
FROM user_shopping_sessions
GROUP BY session_date
ORDER BY session_date
FORMAT Vertical;
```

```response title="Response" theme={null}
Row 1:
──────
session_date:                2024-01-01
new_customer⋯ed_products:    2
returning_customer_products: 3
total_unique_products:       6

Row 2:
──────
session_date:                2024-01-02
new_customer⋯ed_products:    2
returning_customer_products: 2
total_unique_products:       7
```

<div id="see-also">
  ## 관련 항목
</div>

* [`uniq`](/ko/reference/functions/aggregate-functions/uniq)
* [`Array 조합자`](/ko/reference/functions/aggregate-functions/combinators#-array)
* [`If 조합자`](/ko/reference/functions/aggregate-functions/combinators#-if)
