sql_where

用途

explore: view_name_1 {
  join: view_name_2 {
    sql_where: ${view_name_1.id} < 100 ;;
  }
}

Hierarchy
sql_where
Default Value
None

Accepts
A SQL WHERE clause

Definition

sql_where lets you apply a query restriction that users cannot change. The restriction will be inserted into the WHERE clause of the underlying SQL that Looker generates if and only if the join is used in the query. In addition to queries run by human users, the restriction will apply to dashboards, scheduled Looks, and embedded information that relies on that Explore.

The condition can be written in pure SQL, using your database's actual table and column names. It can also use Looker field references like ${view_name.field_name}, which is the preferred method, because Looker can be smarter about automatically including necessary joins. A sql_where condition is not displayed to the user, unless they look at the underlying SQL of any queries that they create.

Example

For example, you can specify that if the join to users is used, that only users younger than 50 should be included:

explore: orders_users_under_50 {
  view_name: orders

  join: users {
    sql_on: ${users.id} = ${orders.user_id} ;;
    sql_where: ${users.age} < 50 ;;
    type: left_outer
  }
}

ユーザーが Orders.CountUsers.Count を選択した場合、この LookML から Looker が生成する SQL は次のようになります。

SELECT
  COUNT(orders.id) AS orders_count,
  COUNT(DISTINCT users.id, 1000) AS users_count
FROM thelook2.orders AS orders
LEFT JOIN thelook2.users AS users ON users.id = orders.user_id

WHERE users.age < 50
LIMIT 500

注意点

OR ロジックを使用する場合は、括弧が必要です

sql_where で OR ロジックを使用する場合は、SQL 条件をかっこで囲むことが非常に重要です。たとえば、次のように記述するのではなく、

sql_where: region = 'Northeast' OR company = 'Altostrat' ;;

次のように記述します。

sql_where: (region = 'Northeast' OR company = 'Altostrat') ;;

この例でかっこを追加し忘れて、ユーザーが独自のフィルタを追加した場合、生成された WHERE 句は次の形式になります。

WHERE
  user_filter = 'something' AND
  region = 'Northeast' OR
  company = 'Altostrat'

この場合、ユーザーが適用したフィルタが機能しないことがあります。AND 条件が最初に評価されるため、company = 'Altostrat' を含む行は必ず表示されます。かっこがない場合、sql_where 条件の一部のみがユーザーのフィルタと組み合わされます。かっこが追加された場合、WHERE 句は次のようになります。

WHERE
  user_filter = 'something' AND
  (region = 'Northeast' OR company = 'Altostrat')

これで、ユーザーのフィルタがすべての行に適用されます。

sql_where 依存関係では結合順序が重要

通常、Looker は LookML で結合が定義されている順序に関係なく、正しい順序で結合を実装します。ただし、sql_where は例外です。sql_where ステートメントで別の結合のフィールドを参照する場合は、参照する結合が LookML の sql_where ステートメントの前に定義されている必要があります。

たとえば、次の sql_where ステートメントは、inventory_items が結合される前に inventory_items.id フィールドを参照しています。

explore: orders {
  hidden: yes
  join: order_items {
    sql_on: ${order_items.order_id} = ${orders.id} ;;
    sql_where: ${inventory_items.id} IS NOT NULL ;;
  }
  join: inventory_items {
    sql_on: ${inventory_items.id}=${order_items.inventory_item_id} ;;
  }
}

この Explore でクエリを実行すると、Looker は inventory_items.id フィールドが見つからないというエラーを返します。

ただし、sql_where ステートメントで参照される結合が sql_where ステートメントの前に定義されるように結合の順序を変更することで、この問題を解決できます。

explore: orders {
  hidden: yes
  join: inventory_items {
    sql_on: ${inventory_items.id}=${order_items.inventory_item_id} ;;
  }
join: order_items {
    sql_on: ${order_items.order_id} = ${orders.id} ;;
    sql_where: ${inventory_items.id} IS NOT NULL ;;
  }
}

inventory_items 結合は、order_items 結合の sql_where ステートメントで inventory_items.id フィールドが参照される前に定義されているため、エラーは発生しません。

sql_where クエリの制限は、結合が使用されている場合にのみ適用されます。

sql_where で指定されたクエリ制限は、結合がクエリで使用されている場合にのみ、Looker が生成する基盤となる SQL の WHERE 句に挿入されます。結合が使用されない場合でも WHERE 句を適用する場合は、代わりに sql_always_where を使用します。