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,Looker 會從這個 LookML 產生下列 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 邏輯,則必須加上括號

如果您使用 OR 邏輯搭配 sql_where,請務必在 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'

在這種情況下,使用者套用的篩選器可能無法運作。無論如何,系統都會顯示含有 company = 'Altostrat' 的資料列,因為系統會先評估 AND 條件。如果沒有括號,只有部分 sql_where 條件會與使用者的篩選器合併。如果加入括號,WHERE 子句會改為如下所示:

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

現在系統會為每個資料列套用使用者的篩選器。

加入順序對於sql_where依附元件很重要

一般來說,無論 LookML 中定義的聯結順序為何,Looker 都會以正確順序實作聯結。但 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} ;;
  }
}

如果您在這個「探索」中執行查詢,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 查詢限制

只有在查詢中使用聯結時,Looker 產生的基礎 SQL 的 WHERE 子句中,才會插入 sql_where 中指定的查詢限制。如要套用 where 子句,即使不會使用聯結也一樣,請改用 sql_always_where