識別符與關鍵字
識別符可以包含英數字元和 _,且不能以數字開頭。它們可以用 . 查找運算子鏈接在一起,用於從欄位擷取值組或從模組擷取變數。
hello
_h3llo
hello.world
this 和 that
this 指的是目前的關聯:
PRQL
from invoices
aggregate (
count this
)
SQL
SELECT
COUNT(*)
FROM
invoices
在 join 中,that 指的是另一個表格:
PRQL
from invoices
join tracks (this.track_id==that.id)
SQL
SELECT
invoices.*,
tracks.*
FROM
invoices
INNER JOIN tracks ON invoices.track_id = tracks.id
this can also be used to remove any column ambiguity. For example, currently using a bare time as a column name will fail, because it’s also a type:
PRQL
from invoices
derive t = time
Error
Error:
╭─[ :2:12 ]
│
2 │ derive t = time
│ ──┬─
│ ╰─── expected a value, but found a type
───╯
但有了 this.time,我們可以移除歧義:
PRQL
from invoices
derive t = this.time
SQL
SELECT
*,
time AS t
FROM
invoices
引述
為了使用其他方式無效的字元,識別符可以用反引號括起來。
編譯為 SQL 時,這些識別符將使用方言特定的引號和引述規則。
PRQL
prql target:sql.mysql
from employees
select `first name`
SQL
SELECT
`first name`
FROM
employees
PRQL
prql target:sql.postgres
from employees
select `first name`
SQL
SELECT
"first name"
FROM
employees
PRQL
prql target:sql.bigquery
from `project-foo.dataset.table`
join `project-bar.dataset.table` (==col_bax)
SQL
SELECT
`project-foo.dataset.table`.*,
`project-bar.dataset.table`.*
FROM
`project-foo.dataset.table`
INNER JOIN `project-bar.dataset.table` ON `project-foo.dataset.table`.col_bax = `project-bar.dataset.table`.col_bax
綱要和資料庫名稱
資料庫表格的識別符可以加上綱要和資料庫名稱的前綴。
PRQL
from my_database.chinook.albums
SQL
SELECT
*
FROM
my_database.chinook.albums
請注意,以下所有識別符都將被視為單獨的表格定義:tracks、public.tracks、my_database.public.tracks。
關鍵字
PRQL 使用了以下關鍵字:
prql- query header more…let- variable definition more…into- variable definition more…case- flow control more…type- 型別宣告func- explicit function declaration more…module- 內部使用internal- 內部使用true- boolean more…false- boolean more…null- NULL more…
關鍵字可以用作識別符(欄位或變數),當被反引號括起來時:`case`。
轉換是 std 命名空間中的普通函式,不是關鍵字。也就是說,std.from 與 from 是相同的函式。在下面的範例中,產生的查詢與沒有 std. 命名空間時相同:
PRQL
std.from my_table
std.select {from = my_table.a, take = my_table.b}
std.take 3
SQL
SELECT
a AS "from",
b AS take
FROM
my_table
LIMIT
3