sql_where

Usage

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.Count ו-Users.Count, ה-SQL ש-Looker ייצור מ-LookML הזה הוא:

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

באופן כללי, Looker מבצע צירופים בסדר הנכון, בלי קשר לסדר שבו הצירופים מוגדרים ב-LookML. החריג לכך הוא sql_where. אם אתם מפנים לשדה מחיבור אחר בהצהרת sql_where, החיבור שאליו אתם מפנים צריך להיות מוגדר לפני הצהרת sql_where ב-LookML.

לדוגמה, הנה הצהרת sql_where שמפנה לשדה inventory_items.id לפני ש-inventory_items הצטרף:

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 מוגדרת לפני שהשדה inventory_items.id מפנה אל הצהרת sql_where ההצטרפות של order_items.

הגבלת השאילתה sql_where חלה רק אם נעשה שימוש ב-join

ההגבלה על השאילתה שצוינה ב-sql_where תווסף לסעיף WHERE של ה-SQL הבסיסי ש-Looker יוצרת אם ורק אם נעשה שימוש ב-join בשאילתה. אם רוצים להחיל משפט where גם אם לא נעשה שימוש ב-join, צריך להשתמש ב-sql_always_where.