sql_distinct_key

사용

view: view_name {
  measure: field_name {
    type: sum_distinct
    sql_distinct_key: ${my_field_name} ;;
  }
}
계층 구조
sql_distinct_key
가능한 필드 유형
측정

수락
SQL 표현식

정의

sql_distinct_key 매개변수는 반복되지 않는 값에 집계를 수행하는 측정 유형(특히 average_distinct, median_distinct, percentile_distinct, sum_distinct 유형의 측정)과 함께 사용됩니다. sql_distinct_key는 Looker에 고유 값을 결정하는 기준으로 사용할 필드를 알려주므로 팬아웃의 경우 잘못된 계산을 방지할 수 있습니다.

예를 들어 type: sum_distinctsql_distinct_key 매개변수로 정의된 고유 값을 기반으로 지정된 필드의 반복되지 않는 값을 합산합니다.

다음과 같은 테이블을 고려해 보겠습니다.

주문 항목 ID 주문 ID 주문 배송
1 1 10.00
2 1 10.00
3 2 20.00
4 2 20.00
5 2 20.00

이 경우 주문마다 여러 행이 있습니다. order_shipping 열에 type: sum의 기본 측정항목을 추가하면 실제로 수집된 총 배송비는 30.00이지만 총계는 80.00이 됩니다.

 # Will NOT calculate the correct shipping amount
measure: total_shipping {
  type: sum
  sql: ${order_shipping} ;;
}

정확한 결과를 얻으려면 sql_distinct_key 매개변수를 사용하여 각 고유 항목 (이 경우 각 고유 주문)을 식별하는 방법을 Looker에 설명하면 됩니다. 이렇게 하면 올바른 금액인 30.00이 계산됩니다.

 # Will calculate the correct shipping amount
measure: total_shipping {
  type: sum_distinct
  sql_distinct_key: ${order_id} ;;
  sql: ${order_shipping} ;;
}

sql_distinct_key의 모든 고유 값에는 sql의 해당 값이 하나만 있어야 합니다. 이 예시가 작동하는 이유는 order_id이 1인 모든 행의 order_shipping이 10.00으로 동일하고, order_id이 2인 모든 행의 order_shipping이 20.00으로 동일하기 때문입니다.