The “Query View” feature in DuneSQL allows you to use an existing query as a view in another query. This powerful functionality enables you to create reusable queries, build complex queries, and take advantage of existing queries as views.
1746191
.
Once you have the queryID, you can use it in your new query using the following syntax:
blockchain
with the value ethereum
, the setup would look like this:
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.
For example:
integers
or booleans
.
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.
For example:
Parameter Type | Syntax | Example |
---|---|---|
Literal | query(tableName='labels.all', whateverKey='lala') | Original Query: select * from {{tableName}} Macro: select * from “query_123(tableName=‘labels.all’)“ |
Integer, Bigint | query(integerKey='1') | Original Query: select {{integerKey}} Macro: select * from “query_123(integerKey=‘1’)“ |
Decimal, Double | query(decimalKey='1.2') query(realKey='1.2F') query(doubleKey='1.2E0') | Original Query: select {{realKey}} Macro: select * from “query_123(realKey=‘1.2F’)“ |
Varchar | query(varcharKey='\'ethereum\'') | Original Query: select * from labels.all where name = {{varcharKey}} Macro: select * from “query_123(varcharKey=”DEX Trader”)“ |
Char | query(charKey='\'a\'') | Original Query: select {{charKey}} Macro: select * from “query_123(charKey=”a”)“ |
Varbinary | query(varbinaryKey='0xabcd') | Original Query: select * from labels.all where address = {{varbinaryKey}} Macro: select * from “query_123(varbinaryKey=‘0xabcd’)“ |
UINT256 | query(uint256Key='UINT256 \'1\'') | Original Query: select * from arbitrum.transactions where gas_price = {{uint256Key}} Macro: select * from “query_123(uint256Key=‘uint256 ‘1”)” Original Query: select * from arbitrum.transactions where gas_price = uint256 ‘ {{uint256Key}} ’ Macro: select * from “query_123(uint256Key=‘1’)“ |
INT256 | query(int256Key='INT256 \'-1\'') | Original Query: select * from arbitrum.transactions where gas_price = {{int256Key}} Macro: select * from “query_123(int256Key=‘int256 ‘-1”)” Original Query: select * from arbitrum.transactions where gas_price = int256 ‘ {{int256Key}} ’ Macro: select * from “query_123(int256Key=‘1’)“ |
Date, Time, Timestamp | query(dateKey='2023-01-02') query(dateKey='date \'2023-01-02\'') query(dateKey='13:45:30 +05:00') query(dateKey='time \'13:45:30 +05:00\'') query(dateKey='2023-08-24 13:45:30 UTC') query(dateKey='timestamp \'2023-08-24 13:45:30 UTC\'') | Original Query: select * from ethereum.blocks where time > {{dateKey}} Macro: select * from “query_123(dateKey=‘timestamp ‘2023-08-24”)” Original Query: select * from ethereum.blocks where time > timestamp ‘ {{dateKey}} ’ Macro: select * from “query_123(dateKey=‘2023-08-24’)“ |
Array | query(arrayKey='array[1,2,3]') query(arrayKey='array[uint256 \'1\', uint256 \'2\']') query(arrayKey='array[\'these\', \'are\, \'varchar\]') | Original Query: select * from dex_aggregator.trades where trace_address = {{arrayKey}} Macro: select * from “query_123(arrayKey=‘array[1, 2]’)“ |
Boolean | query(booleanKey='true') | Original Query: select * from bitcoin.inputs where is_coinbase = {{booleanKey}} Macro: select * from “query_123(booleanKey=‘false’)“ |
Row | query(rowKey='row(1, uint256 \'1\', \'hi\')') | Original Query: select {{rowKey}} Macro: select * from “query_123(rowKey=‘row(false, 1, uint256 ‘1’, ‘hi’)’)“ |
Map | query(mapKey='map(array[\'key1\', \'key2\'], array[\'value1\', \'value2\'])') | Original Query: select {{mapKey}} Macro: select * from “query_123(mapKey=‘map(array[‘key1’, ‘key2’], array[‘value1’, ‘value2’])’)“ |
select 1
or select count(*) from ethereum.transactions
, but you can query select 1 as v
and select count(*) as total from ethereum.transactions
.select "query_123(Key=\'VaLue\')"
will not work).