메인 콘텐츠로 건너뛰기

설명

ArrayIf 조합자는 uniq 함수에 적용할 수 있으며, 집계 조합자 함수인 uniqArrayIf를 사용하면 조건이 true인 행의 배열에서 고유한 값의 수를 계산할 수 있습니다.
-If와 -Array는 함께 사용할 수 있습니다. 단, Array가 먼저 오고 그다음에 If가 와야 합니다.
이 함수는 arrayJoin을 사용하지 않고도 특정 조건에 따라 배열의 고유 요소 수를 계산해야 할 때 유용합니다.

사용 예시

세그먼트 유형과 참여 수준별로 조회된 고유 제품 수 계산

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

관련 항목

마지막 수정일 2026년 6월 10일