Uniq
The uniq function is used to estimate the count of unique values in a dataset. It's particularly useful for analyzing large datasets where an exact count may not be necessary or where performance is a concern. Below is an example query using the uniq function with the EVM Holders API to get the number of unique token holders for a specific token on a given date.
Example Query
{
EVM(dataset: archive, network: eth) {
Holders(
date: "2024-03-04"
where: { Currency: { SmartContract: { is: "0x95AD61B0A150D79219DCF64E1E6CC01F0B64C4CE" } }, Balance: {Amount: {gt: "0"}}}
) {
exact: uniq(of: Holder_Address, method: exact)
count(distinct: Holder_Address)
approximate: uniq(of: Holder_Address, method: approximate)
}
}
}
Result
{
"EVM": {
"Holders": [
{
"approximate": "1373089",
"count": "1378364",
"exact": "1378364"
}
]
}
}
Understanding Uniq Function Variants
- Estimate (
uniq): Theuniqfunction estimates the count of unique values. It uses an adaptive sampling algorithm. This approach is highly accurate and CPU-efficient for processing large datasets. - Exact Count (
uniqExact): For scenarios requiring precision, theuniqExactfunction calculates the exact number of unique values. Although more memory-intensive it guarantees accuracy.
When to Use Each Variant
- Use
uniqfor Estimates: Opt for theuniqfunction when an approximate count suffices. This function is ideal for large datasets where performance and efficiency are priorities. - Use
uniqExactfor Precision: Choose theuniqExactfunction when accuracy is non-negotiable. Keep in mind the potential for increased memory usage.
Uniq on a list of fields
of also accepts a list of fields. The metric then counts the distinct combinations of those fields, not the distinct values of each one.
This query counts the distinct buy-mint and sell-mint combinations traded on Solana DEXs on 1 October 2026. count(distinct: ...) accepts the same list and returns the same number as uniq with method: exact.
query ($fields: [Solana_DEXTrade_EnumFields!]) {
Solana(dataset: archive) {
DEXTrades(where: {Block: {Date: {is: "2026-10-01"}}}) {
count(distinct: $fields)
uniq(
of: [Trade_Buy_Currency_MintAddress, Trade_Sell_Currency_MintAddress]
method: exact
)
}
}
}
Variables:
{
"fields": ["Trade_Buy_Currency_MintAddress", "Trade_Sell_Currency_MintAddress"]
}
Result
{
"Solana": {
"DEXTrades": [
{
"count": "402072",
"uniq": "402072"
}
]
}
}
- Inline or variable: write the list inline, or pass it as a variable of type
[<Cube>_EnumFields!], for example[Solana_DEXTrade_EnumFields!]or[EVM_DEXTrade_EnumFields!]. - Combinations, not a union: over a 10-minute window the same query returned 2,750 distinct buy mints, 5,148 distinct sell mints and 11,614 distinct combinations of the two.
- Single field still works:
uniq(of: Trade_Buy_Currency_MintAddress)anduniq(of: [Trade_Buy_Currency_MintAddress])return the same number. methodworks the same way with a list:exactfor a precise count,approximate(the default) for an estimate.- It works on other cubes and chains too, such as
EVM.DEXTrades,EVM.TransfersandSolana.DEXTradeByTokens.
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.