davidwallUntitled Query
    Updated 2023-01-19
    --credit : https://app.flipsidecrypto.com/velocity/queries/aa82f866-0a25-4751-8abe-9a8d4f2eee9f
    with lunapricet as (
    select date_trunc (hour,block_timestamp) as date1,
    median (to_amount/from_amount) as LUNA_Price
    from terra.core.ez_swaps
    where from_currency = 'uluna'
    and to_currency = 'ibc/B3504E092456BA618CC28AC671A71FB08C6CA0FD0BE7C8A5B5A3E2DD933CC9E4'
    and to_amount < 1e8 and from_amount < 1e8
    and block_timestamp >= '2023-01-01'
    group by 1 having LUNA_Price < 2 and LUNA_Price > 1.22 and LUNA_Price != 1.333487
    order by 1),

    cextable as (
    select distinct address from terra.core.dim_address_labels where label_type = 'cex'
    union ALL
    select distinct address from crosschain.core.address_labels where label_type = 'cex' and blockchain = 'terra'),

    maintable as (
    select date_trunc (hour,block_timestamp) as date,
    LUNA_Price,
    case when block_timestamp::date = '2023-01-09' then '9 January'
    else 'Other Days' end as timespan,
    case when receiver = address then 'Transfer To CEXs'
    when sender = address then 'Transfer From CEXs'
    else null end as transfer_type1,
    count (distinct tx_id) as TX_Count,
    sum (amount) as Volume,
    avg (amount) as Average_Volume
    from terra.core.ez_transfers t1 join cextable t2 on t1.receiver = t2.address or t1.sender = t2.address
    join lunapricet t3 on date_trunc(hour,t1.block_timestamp) = t3.date1
    where tx_succeeded = 'TRUE'
    and block_timestamp >= '2023-01-01'
    and transfer_Type1 is not null
    and currency = 'uluna'
    group by 1,2,3,4)

    Run a query to Download Data