Query editor
-- This query used incompatible data types from Dune SQL alpha and may need to be updated.-- More details can be found on https://dune.com/docs/query/dunesql-changes/with trades as(SelectCOALESCE(token_sold_symbol,cast(token_sold_address as varchar)) as token_sold, COALESCE(token_bought_symbol, cast(token_bought_address as varchar)) as token_bought, token_pair, token_sold_amount, token_bought_amount, amount_usd, date_trunc('{{1. Date Rollup Frequency a}}', block_time) as time, block_time, tx_hash-- Stablecoin sales category, Case when lower(token_sold_symbol) like '%usdc%' then 'stablecoin_sold'when lower(token_sold_symbol) like '%usd%' then 'stablecoin_sold'when lower(token_sold_symbol) like '%usdt%' then 'stablecoin_sold'when lower(token_sold_symbol) like '%busd%' then 'tablecoin_sold'when lower(token_sold_symbol) like '%dai%' then 'stablecoin_sold'Else 'Coin'End As stable_sold-- Stablecoin buy category, Case when lower(token_bought_symbol) like '%usdc%' then 'stablecoin_bought'when lower(token_bought_symbol) like '%usdt%' then 'stablecoin_bought'when lower(token_bought_symbol) like '%busd%' then 'stablecoin_bought'when lower(token_bought_symbol) like '%dai%' then 'stablecoin_bought'Else 'Coin'End As stable_bought, p.contract_address, p.median_pricefrom dex.tradesLeft Join dex.prices as p ON p.hour = block_time), stable_sales as (Selecttime, sum(amount_usd) as sum_sales, count(stable_sold) as count_salesFrom tradeswhere stable_sold = 'stablecoin_sold'AND stable_bought != 'stablecoin_bought'AND token_bought not in ('TBTC','WIRTUAL','WHALE','aBNBc', 'UST', 'HAY', 'TIME')AND block_time > now() - interval '{{1. Date Frequency}}' {{1. Date Period a}}group by 1), stable_buys as (Selecttime, sum(amount_usd) as sum_buys, count(stable_bought) as count_buysFrom tradeswhere stable_bought = 'stablecoin_bought'AND stable_sold != 'stablecoin_sold'AND token_bought not in ('TBTC','WIRTUAL','WHALE','aBNBc', 'UST', 'HAY', 'TIME')AND block_time > now() - interval '{{1. Date Frequency}}' {{1. Date Period a}}group by 1)