用途
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 であるためです。