篩選行
在前一頁,我們學到了 select、derive 和 join 如何改變表的列。
現在我們將探索如何使用 filter 和 take 操作表的行。
filter 變換
filter 變換根據值選擇要通過的行:
from invoices
filter billing_city == "Berlin"
結果表包含來自柏林的所有行。
PRQL 將單個 filter 變換轉換為使用適當的 SQL WHERE 或 HAVING 命令,取決於它在管道中出現的位置。
take 變換
take 變換根據行在表中的位置選擇要通過的行。所選行的集合可以用兩種方式指定:
- 一個純數字
x,將選擇前x行,或 - 行的包含範圍
start..end。
from invoices
take 4
from invoices
take 4..7
當然,可以將所有這些變換組合到一個管道中:
from invoices
# retain only rows for orders from Berlin
filter billing_city == "Berlin"
# skip first 10 rows and take the next 10
take 11..20
# take only first 3 rows of *that* result
take 3
在最後我們做了一些有點奇怪的事情:首先我們取了行 11..20,然後從該結果中取前 3 行。
[!NOTE] Note that a single transform
take 11..13would have produced the same SQL. The example shows how PRQL allows fast data exploration by “stacking” transforms in the pipeline. This reduces the cognitive burden: unlike SQL, each new transform interacts only with the results of the previous query.