You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/query/query-a-query.md
+75-4Lines changed: 75 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,8 @@ The "Query View" feature in DuneSQL allows you to use an existing query as a vie
10
10
**You can also pass on parameters when querying a query view.**
11
11
12
12
!!! info
13
-
All upstream queries are executed as functional SQL views, which means that they will be executed every time they are queried. This means there currently is no performance benefit to using the "Query a Query" feature.
13
+
All upstream queries are executed as functional SQL views, which means that they will be executed every time they are queried. This means there currently is no performance benefit to using the "Query a Query" feature. If you are looking for a performance benefit, consider using [materialized views](materialized-views.md) instead.
14
+
14
15
## Using Query View
15
16
16
17
To use the "Query View" feature, you will need the queryID of the query you want to use as a view. The queryID can be found in the URL of a query. For example, if the URL of a query is [https://dune.com/queries/1746191](https://dune.com/queries/1746191), the queryID would be `1746191`.
@@ -35,16 +36,86 @@ To pass on parameters when querying a query view, you need to use the following
For example, if you want to use the query with queryID 3256410 as a view in your new query and pass on the parameter `blockchain` with the value `ethereum`, the setup would look like this:
43
+
44
+
```sql
45
+
--query_3256410, our query to be invoked
46
+
Select
47
+
project,
48
+
block_date,
49
+
sum(amount_usd) as amount_usd
50
+
fromdex.trades
51
+
where blockchain ='{{blockchain}}'
52
+
and block_time > now() - interval '7' day
53
+
```
54
+
[link to query](https://dune.com/queries/3256410)
55
+
56
+
```sql
57
+
--query_3256401, our query that invokes the query_3256410
58
+
Select
59
+
project,
60
+
block_date,
61
+
amount_usd
62
+
from"query_3256410(blockchain='ethereum')"
63
+
```
64
+
[link to query](https://dune.com/queries/3256401)
65
+
66
+
Parameters in the query view are passed on as strings and therefore always need to be wrapped in single quotes. Since we sometimes want to pass a `string` including the single quotes, we need to escape the single quotes in the query view. We can do that by adding a backslash in front of the single quote. Escaping single quotes means that the single quote will be treated as a literal character and not as a string delimiter.
67
+
68
+
**For example:**
69
+
70
+
```sql
71
+
--query to be invoked
72
+
Select
73
+
project,
74
+
block_date,
75
+
sum(amount_usd) as volume_in_usd
76
+
fromdex.trades
77
+
where blockchain = {{blockchain}}
78
+
-- we want to pass a string including the single quotes to this query
39
79
```
40
80
81
+
```sql
82
+
--query that invokes the query above
83
+
Select
84
+
project,
85
+
block_date,
86
+
volume_in_usd
87
+
from"query_3256410(blockchain='\'ethereum\'')"
88
+
--note the escaped single quotes
89
+
--this will pass the parameter blockchain with the value 'ethereum'
90
+
```
41
91
42
-
The table below shows the syntax for passing parameters to different types when querying a query view.
92
+
The backslash is not needed when passing on a parameter that doesn't need to be wrapped in single quotes on the receiving side, like `integers` or `booleans`.
93
+
You can choose to handle this on the query view side or on the query that invokes the query view side. If you wrap your parameter in single quotes on the receiving side, you don't need to escape the single quotes on the query view side.
43
94
95
+
**For example:**
44
96
97
+
```sql
98
+
--query_3256410, our query to be invoked
99
+
Select
100
+
project,
101
+
block_date,
102
+
sum(amount_usd) as amount_usd
103
+
fromdex.trades
104
+
where blockchain ='{{blockchain}}'
105
+
and block_time > now() - interval '7' day
106
+
--note the single quotes around the parameter blockchain
107
+
```
45
108
46
-
For `varchar`, `uint256`, `int256` and `datetime` parameters, you should use single quotes wrapping the params on the base query.
109
+
```sql
110
+
Select
111
+
project,
112
+
block_date,
113
+
amount_usd
114
+
from"query_3256410(blockchain='ethereum')"
115
+
--since we wrap the parameter blockchain in single quotes on the receiving side, we don't need to escape the single quotes on the query view side
116
+
```
47
117
118
+
The table below shows how different parameter types are passed on when using the "Query view" feature:
0 commit comments