File tree Expand file tree Collapse file tree 2 files changed +47
-4
lines changed Expand file tree Collapse file tree 2 files changed +47
-4
lines changed Original file line number Diff line number Diff line change 1
1
# Variables
2
2
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 = ` :
4
9
5
10
``` prql
6
11
let top_50 = (
@@ -29,13 +34,27 @@ let grouping = s"""
29
34
from grouping
30
35
```
31
36
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
+
32
51
``` 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
34
53
of CTEs can be represented with a single pipeline.
35
54
```
36
55
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.
39
58
40
59
<!--
41
60
, like recursive queries:
Original file line number Diff line number Diff line change
1
+ -- -
2
+ source : web / book / tests / snapshot .rs
3
+ expression : " from employees\n sort salary\n take 50\n aggregate [total_salary = sum salary]\n into top_50\n\n from 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
+
You can’t perform that action at this time.
0 commit comments