cryptallTop Swap Path on Thorchain (Count) copy
    Updated 2024-06-24
    -- forked from banbannard / Top Swap Path on Thorchain (Count) @ https://flipsidecrypto.xyz/banbannard/q/lGzhEw9VdyAU/top-swap-path-on-thorchain-count

    with base as (select tx_id,
    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 >= '2024-01-01'
    --where tx_id = 'E3701AEC766A1CCBE22F0857EBEEE36BC2A358684A7577AC9900D3FEA99DD1A3'
    group by tx_id, from_asset, to_asset),

    base2 as (select tx_id,
    array_agg(distinct assets) within group (order by assets asc) as swap_direction,
    sum(swap_volume_usd) as swap_volume
    from base
    group by 1),

    base3 as (select swap_direction[0] as path1,
    swap_direction[1] as path2,
    case
    when path2 is null then path1
    when substr(path1, 1, 4) = 'RUNE' then path2
    else path1
    end as swap_path1,
    QueryRunArchived: QueryRun has been archived