sql_where

Utilizzo

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
  }
}

Se l'utente seleziona Orders.Count e Users.Count, il codice SQL che Looker genererebbe da questo codice 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

Aspetti da considerare

Le parentesi sono obbligatorie se utilizzi la logica OR

Se utilizzi la logica OR con sql_where, è molto importante inserire le parentesi intorno alla condizione SQL. Ad esempio, invece di scrivere:

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

Scriveresti:

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

Se in questo esempio hai dimenticato di aggiungere le parentesi e un utente ha aggiunto il proprio filtro, la clausola WHERE generata potrebbe avere il seguente formato:

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

In questa situazione, il filtro applicato dall'utente potrebbe non funzionare. In ogni caso, le righe con company = 'Altostrat' verranno visualizzate perché la condizione AND viene valutata per prima. Senza parentesi, solo una parte della condizione sql_where si combina con il filtro dell'utente. Se sono state aggiunte parentesi, la clausola WHERE avrà invece questo aspetto:

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

Ora il filtro dell'utente verrà applicato a ogni riga.

L'ordine di unione è importante per le dipendenze sql_where

In generale, Looker implementa i join nell'ordine corretto, indipendentemente dall'ordine in cui sono definiti in LookML. L'eccezione è sql_where. Se fai riferimento a un campo di un'altra unione nell'istruzione sql_where, l'unione a cui fai riferimento deve essere definita prima dell'istruzione sql_where in LookML.

Ad esempio, ecco un'istruzione sql_where che fa riferimento al campo inventory_items.id prima dell'unione di 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} ;;
  }
}

Se esegui una query in questa esplorazione, Looker restituirà un errore che indica che non è possibile trovare il campo inventory_items.id.

Tuttavia, puoi risolvere il problema riordinando i join in modo che il join a cui viene fatto riferimento nell'istruzione sql_where sia definito prima dell'istruzione sql_where, come segue:

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 ;;
  }
}

In questo modo l'errore non si verifica perché l'unione inventory_items è definita prima che venga fatto riferimento al campo inventory_items.id nell'istruzione sql_where dell'unione order_items.

La limitazione della query sql_where viene applicata solo se viene utilizzato il join

La limitazione della query specificata in sql_where verrà inserita nella clausola WHERE dell'SQL sottostante generato da Looker se e solo se il join viene utilizzato nella query. Se vuoi che venga applicata una clausola WHERE anche se il join non sarebbe stato utilizzato, usa sql_always_where.