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 로직을 사용하는 경우 괄호가 필요합니다.

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 문 앞에 정의되어야 합니다.

예를 들어 다음은 inventory_items가 조인되기 전에 inventory_items.id 필드를 참조하는 sql_where 문입니다.

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를 사용하세요.