Skip to main content

Count Distinct

count with distinct attribute calculates the unique values in each set of dimensions. This query will count miners (unique coinbase) by every date:

{
EVM (dataset: archive){
Blocks {
Block {
Date
}
miners: count(distinct: Block_Coinbase)
}
}
}

Count distinct on a list of fields​

distinct also accepts a list of fields. The metric then counts the distinct combinations of those fields in each set of dimensions.

This query counts the distinct buy-token and sell-token combinations traded on Ethereum DEXs in the last hour, next to the distinct count of each side on its own:

{
EVM(network: eth) {
DEXTrades(where: {Block: {Time: {since_relative: {hours_ago: 1}}}}) {
pairs: count(
distinct: [Trade_Buy_Currency_SmartContract, Trade_Sell_Currency_SmartContract]
)
buy_tokens: count(distinct: Trade_Buy_Currency_SmartContract)
sell_tokens: count(distinct: Trade_Sell_Currency_SmartContract)
}
}
}

A sample result shows that the list counts combinations, not the two fields added together:

{
"EVM": {
"DEXTrades": [
{
"buy_tokens": "1019",
"pairs": "2745",
"sell_tokens": "918"
}
]
}
}

The list can also be passed as a variable of type [<Cube>_EnumFields!]:

query ($fields: [EVM_DEXTrade_EnumFields!]) {
EVM(network: eth) {
DEXTrades(where: {Block: {Time: {since_relative: {hours_ago: 1}}}}) {
pairs: count(distinct: $fields)
}
}
}
{
"fields": ["Trade_Buy_Currency_SmartContract", "Trade_Sell_Currency_SmartContract"]
}

The if argument works with a list as well. uniq accepts the same list in its of argument.

Build with Bitquery

Ready to run this in production?

Get an API key and run these queries in minutes, or talk to us about plans and enterprise delivery.