mogarchySS TX Swap Example copy
    Updated 2023-10-13
    -- forked from banbannard / SS TX Swap Example @ https://flipsidecrypto.xyz/banbannard/q/LOTzdt0SEkau/ss-tx-swap-example

    with base as (select tx_id,
    date_trunc('day', block_timestamp) as day,
    affiliate_address, affiliate_fee_basis_points,
    split(from_asset, '-')[0] as from_assets,
    case
    when from_assets ilike '%/%' then split(from_assets, '/')[1]
    else split(from_assets, '.')[1]
    end as from_asset_names,
    split(to_asset, '-')[0] as to_assets,
    case
    when to_assets ilike '%/%' then split(to_assets, '/')[1]
    else split(to_assets, '.')[1]
    end as to_asset_names,
    concat(from_asset_names, ' -> ', to_asset_names) as assets,
    case when assets ilike '%RUNE' then 2
    else 1
    end as numbering,
    sum(to_amount_usd) as swap_volume_usd
    from thorchain.defi.fact_swaps
    where block_timestamp >= '2022-01-01'
    and tx_id in (select tx_id from thorchain.defi.fact_swaps_events where memo ilike '%/%/%')
    group by tx_id, day,
    affiliate_address, affiliate_fee_basis_points,
    from_asset, to_asset),

    base2 as (select day,
    tx_id,
    affiliate_address, affiliate_fee_basis_points,
    array_agg(distinct assets) within group (order by assets asc) as swap_direction, --merging 2 sep path to 1
    sum(swap_volume_usd) as swap_volume
    from base
    group by 1,2,3,4),

    base3 as (select day,
    Run a query to Download Data