Skip to content

Commit 6465777

Browse files
authored
docs: Add docs for into (PRQL#2597)
1 parent 07d83ca commit 6465777

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

web/book/src/queries/variables.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Variables
22

3-
We can define a relation — similar to a CTE in SQL — as a variable with `let`:
3+
We can define a relation — similar to a CTE in SQL — with two approaches — a
4+
prefix `let` or a postfix `into.
5+
6+
## `let`
7+
8+
Here we assign a variable to `foo` with `let` by prefixing with `let foo =`:
49

510
```prql
611
let top_50 = (
@@ -29,13 +34,27 @@ let grouping = s"""
2934
from grouping
3035
```
3136

37+
## `into`
38+
39+
We can also assign a variable to `foo` by postfixing with `into foo`:
40+
41+
```prql
42+
from employees
43+
sort salary
44+
take 50
45+
aggregate [total_salary = sum salary]
46+
into top_50
47+
48+
from top_50 # Starts a new pipeline
49+
```
50+
3251
```admonish info
33-
In PRQL `table`s are far less common than CTEs are in SQL, since a linear series
52+
In PRQL variables are far less common than CTEs are in SQL, since a linear series
3453
of CTEs can be represented with a single pipeline.
3554
```
3655

37-
Currently defining variables with `let` is restricted to relations. We'd like to
38-
extend this to expressions that evaluate to scalars.
56+
Currently defining variables is restricted to relations. We'd like to extend
57+
this to expressions that evaluate to scalars.
3958

4059
<!--
4160
, like recursive queries:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: web/book/tests/snapshot.rs
3+
expression: "from employees\nsort salary\ntake 50\naggregate [total_salary = sum salary]\ninto top_50\n\nfrom top_50 # Starts a new pipeline\n"
4+
---
5+
WITH table_1 AS (
6+
SELECT
7+
salary
8+
FROM
9+
employees
10+
ORDER BY
11+
salary
12+
LIMIT
13+
50
14+
), top_50 AS (
15+
SELECT
16+
SUM(salary) AS total_salary
17+
FROM
18+
table_1 AS table_0
19+
)
20+
SELECT
21+
total_salary
22+
FROM
23+
top_50
24+

0 commit comments

Comments
 (0)