Skip to content

Commit 0f2a8ef

Browse files
committed
add more description to querying a view with parameters
1 parent f7c0c4b commit 0f2a8ef

File tree

1 file changed

+75
-4
lines changed

1 file changed

+75
-4
lines changed

docs/query/query-a-query.md

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ The "Query View" feature in DuneSQL allows you to use an existing query as a vie
1010
**You can also pass on parameters when querying a query view.**
1111

1212
!!! 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+
1415
## Using Query View
1516

1617
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
3536

3637
```sql
3738

38-
Select * from query_<queryID>(<parameter1>=<value1>, <parameter2>=<value2>, ...)
39+
Select * from "query_<queryID>(<parameter1>='<value1>', <parameter2>='<value2>', ...)"
40+
```
41+
42+
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+
from dex.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+
from dex.trades
77+
where blockchain = {{blockchain}}
78+
-- we want to pass a string including the single quotes to this query
3979
```
4080

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+
```
4191

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.
4394

95+
**For example:**
4496

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+
from dex.trades
104+
where blockchain = '{{blockchain}}'
105+
and block_time > now() - interval '7' day
106+
--note the single quotes around the parameter blockchain
107+
```
45108

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+
```
47117

118+
The table below shows how different parameter types are passed on when using the "Query view" feature:
48119

49120

50121
| **Parameter Type** | **Syntax** | **Example** |

0 commit comments

Comments
 (0)