Query editor
withstatic as (select'month' as timebox, map_from_entries(array[ -- percentile which is the upper bound of the interval & interval name(0.25, 'Low'), (0.65, 'Middle'), (0.95, 'Upper'), (0.99, 'Power'), (1.00, 'Whale')]) as groups), users as (select*, (select groups from static) as groups, cardinality((select groups from static)) as groups_num, approx_percentile(volume, map_keys((select groups from static))) over() as percentilesfrom (selectdate_trunc((select timebox from static), block_time) as time, user, sum(amount_usd) as volume, count(*) as swapsfrom oneinch.swapswhereprotocol = 'AR'and tx_success and call_successand if('{{blockchain}}' = '= all =', true, blockchain = '{{blockchain}}')and amount_usd is not nullgroup by 1, 2))-- main --selectreduce(sequence(groups_num, 1), '', (r, x) -> if(volume <= percentiles[x], cast(x as varchar) || ' ' || map_values(groups)[x], r), r -> r) as segment, reduce(sequence(groups_num, 1), '', (r, x) -> if(volume <= percentiles[x], if(x > 1, format('$%,.0f', percentiles[x - 1]) || format(' <> $%,.0f', percentiles[x]), format('$0 <> $%,.0f', percentiles[x])), r), r -> r) as segment_interval, reduce(sequence(groups_num, 1), '', (r, x) -> if(volume <= percentiles[x], if(x > 1, format('%,.0f%%', (map_keys(groups)[x] - map_keys(groups)[x - 1]) * 100) || format(', from %,.0f', map_keys(groups)[x - 1] * 100) || format(' to %,.0f percentiles', map_keys(groups)[x] * 100), format('%,.0f%%', map_keys(groups)[x] * 100) || format(', from 0 to %,.0f percentiles', map_keys(groups)[x] * 100)), r), r -> r) as segment_interval_percents, transform(sequence(1, groups_num), x -> cast(map_keys(groups)[x] as varchar) || ': ' || format('$%,.0f', percentiles[x])) as borders, sum(swaps) as swaps, count(distinct user) as users, sum(volume) as volume, avg(swaps) as avg_monthly_swaps, avg(volume) as avg_monthly_volumefrom usersgroup by 1, 2, 3, 4order by 1 desc