select blockchain,
project,
project || '_' || blockchain || '.trades' as spell_table_name,
count(*) as trade_count
from dex.trades
group by 1, 2, 3
order by 1 asc, 4 desc
select block_date,
count(*) as trade_count,
count(distinct taker) as active_user_count,
sum(amount_usd) as trade_amount
from dex.trades
where block_date >= date('2022-10-01')
group by 1
order by 1
with trade_summary as (
select block_date,
count(*) as trade_count,
count(distinct taker) as active_user_count,
sum(amount_usd) as trade_amount
from dex.trades
where blockchain = 'ethereum'
and block_date >= date('2021-01-01')
and token_pair <> 'POP-WETH' -- Exclude outlier that has wrong amount
group by 1
order by 1
),
user_initial_trade as (
select taker,
min(block_date) as initial_trade_date
from dex.trades
where blockchain = 'ethereum'
and block_date >= date('2021-01-01')
and token_pair <> 'POP-WETH' -- Exclude outlier that has wrong amount
group by 1
),
new_user_summary as (
select initial_trade_date,
count(taker) as new_user_count
from user_initial_trade
group by 1
order by 1
)
select t.block_date,
trade_count,
active_user_count,
trade_amount,
new_user_count,
active_user_count - new_user_count as existing_user_count,
sum(trade_count) over (order by t.block_date) as accumulate_trade_count,
sum(trade_amount) over (order by t.block_date) as accumulate_trade_amount,
sum(new_user_count) over (order by u.initial_trade_date) as accumulate_new_user_count,
(sum(trade_count) over ()) / 1e6 as total_trade_count,
(sum(trade_amount) over ()) / 1e9 total_trade_amount,
(sum(new_user_count) over ()) / 1e6 as total_new_user_count
from trade_summary t
left join new_user_summary u on t.block_date = u.initial_trade_date
order by t.block_date
select block_date,
project,
count(*) as trade_count,
count(distinct taker) as active_user_count,
sum(amount_usd) as trade_amount
from dex.trades
where blockchain = 'ethereum'
and block_date >= date('2021-01-01')
and token_pair <> 'POP-WETH' -- Exclude outlier that has wrong amount
group by 1, 2
order by 1, 2
这里只对比活跃用户数量、交易数量和交易金额。分别针对结果集的不同字段添加条形图和饼图,加入数据看板。你可能已经注意到,我们的查询结果数据是按天和项目两个维度进行汇总的。当我们创建Pie Chart 饼图图表时,如果只选择 Project 维度作为 X Column,选择 trade_count 为 Y Column 1,不选择Group By分组的字段,此时每一天的trade_count值会自动被累加到一起,其总和值被展示在饼图中。这样我们就不用单独写一个查询来生成饼图了。这也算是一个应用技巧。数据看板的显示效果如下图:
with top_token_pair as (
select token_pair,
count(*) as transaction_count
from dex.trades
where blockchain = 'ethereum'
and block_date >= date('2021-01-01')
and token_pair <> 'POP-WETH' -- Exclude outlier that has wrong amount
group by 1
order by 2 desc
limit 20
)
select date_trunc('month', block_date) as block_date,
token_pair,
count(*) as trade_count,
count(distinct taker) as active_user_count,
sum(amount_usd) as trade_amount
from dex.trades
where blockchain = 'ethereum'
and block_date >= date('2021-01-01')
and token_pair in (
select token_pair from top_token_pair
)
group by 1, 2
order by 1, 2
select block_date,
count(*) as trade_count,
count(distinct taker) as active_user_count,
sum(amount_usd) as trade_amount
from uniswap_ethereum.trades
where block_date >= date('2022-01-01')
group by 1
order by 1
with top_token_pair as (
select token_pair,
count(*) as transaction_count
from uniswap_ethereum.trades
where blockchain = 'ethereum'
and block_date >= date('2022-01-01')
group by 1
order by 2 desc
limit 20
)
select date_trunc('month', block_date) as block_date,
token_pair,
count(*) as trade_count,
count(distinct taker) as active_user_count,
sum(amount_usd) as trade_amount
from uniswap_ethereum.trades
where blockchain = 'ethereum'
and block_date >= date('2022-01-01')
and token_pair in (
select token_pair from top_token_pair
)
group by 1, 2
order by 1, 2
with monthly_active_user as (
select distinct taker as address,
date_trunc('month', block_date) as active_trade_month
from uniswap_v3_ethereum.trades
),
user_initial_trade as (
select taker as address,
min(date_trunc('month', block_date)) as initial_trade_month
from uniswap_v3_ethereum.trades
group by 1
),
user_status_detail as (
select coalesce(c.active_trade_month, date_trunc('month', p.active_trade_month + interval '45' day)) as trade_month,
coalesce(c.address, p.address) as address,
(case when n.address is not null then 1 else 0 end) as is_new,
(case when n.address is null and c.address is not null and p.address is not null then 1 else 0 end) as is_retained,
(case when n.address is null and c.address is null and p.address is not null then 1 else 0 end) as is_churned,
(case when n.address is null and c.address is not null and p.address is null then 1 else 0 end) as is_returned
from monthly_active_user c
full join monthly_active_user p on p.address = c.address and p.active_trade_month = date_trunc('month', c.active_trade_month - interval '5' day)
left join user_initial_trade n on n.address = c.address and n.initial_trade_month = c.active_trade_month
where coalesce(c.active_trade_month, date_trunc('month', p.active_trade_month + interval '45' day)) < current_date
),
user_status_summary as (
select trade_month,
address,
(case when sum(is_new) >= 1 then 'New'
when sum(is_retained) >= 1 then 'Retained'
when sum(is_churned) >= 1 then 'Churned'
when sum(is_returned) >= 1 then 'Returned'
end) as user_status
from user_status_detail
group by 1, 2
),
monthly_summary as (
select trade_month,
user_status,
count(address) as user_count
from user_status_summary
group by 1, 2
)
select trade_month,
user_status,
(case when user_status = 'Churned' then -1 * user_count else user_count end) as user_count
from monthly_summary
order by 1, 2