Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

管道

管道是組成管道的轉換之間的連接。管道前的轉換所產生的關聯用作管道後轉換的輸入。管道可以用換行符或管道字元 (|) 表示。

例如,這裡 filter 轉換對 from employees(這只是 employees 表格)的結果進行操作,select 轉換對 filter 轉換的結果進行操作。

PRQL

from employees
filter department == "Product"
select {first_name, last_name}

SQL

SELECT
  first_name,
  last_name
FROM
  employees
WHERE
  department = 'Product'

In the place of a line break, it’s also possible to use the | character to pipe results between transforms, such that this is equivalent:

PRQL

from employees | filter department == "Product" | select {first_name, last_name}

SQL

SELECT
  first_name,
  last_name
FROM
  employees
WHERE
  department = 'Product'

“Ceci n’est pas une pipe”

In almost all situations, a line break acts as a pipe. But there are a few cases where a line break doesn’t act as a pipe.

  • 在元組項目之前或之後
  • 在列表項目之前或之後
  • 在新陳述句之前,它以 letfrom(或 func)開頭
  • 換行

For example:

PRQL

[
  {a=2}      # No pipe from line break before & after this list item
]
derive {
  c = 2 * a, # No pipe from line break before & after this tuple item
}

SQL

WITH table_0 AS (
  SELECT
    2 AS a
)
SELECT
  a,
  2 * a AS c
FROM
  table_0

PRQL

let b =
  \ 3        # No pipe from line break within this line wrap

# No pipe from line break before this `from` statement

from y
derive a = b

SQL

SELECT
  *,
  3 AS a
FROM
  y

Inner Transforms

Parentheses are also used for transforms (such as group and window) that pass their result to an “inner transform”. The example below applies the aggregate pipeline to each group of unique title and country values:

PRQL

from employees
group {title, country} (
  aggregate {
    average salary,
    ct = count salary,
  }
)

SQL

SELECT
  title,
  country,
  AVG(salary),
  COUNT(*) AS ct
FROM
  employees
GROUP BY
  title,
  country