用途
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_distinct は、指定されたフィールドの繰り返されない値を、sql_distinct_key パラメータで定義された一意の値に基づいて合計します。
次のようなテーブルについて考えます。
| Order Item 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 に対応する値が 1 つだけ存在する必要があります。この例が機能するのは、すべての order_id が 1 の行の order_shipping が 10.00 で、すべての order_id が 2 の行の order_shipping が 20.00 であるためです。