Jor-elRUNE Flow from CEXes copy copy
    Updated 2024-10-07
    -- forked from Deebs-DeFi-j9fRbz / RUNE Flow from CEXes copy @ https://flipsidecrypto.xyz/Deebs-DeFi-j9fRbz/q/KGp5l82PizDz/rune-flow-from-cexes-copy

    -- forked from RUNE Flow from CEXes @ https://flipsidecrypto.xyz/edit/queries/5541675a-efcc-402e-b10b-eb7811231dc6

    With flows as(
    SELECT
    CASE
    WHEN FROM_ADDRESS IN (select address from thorchain.core.dim_labels WHERE label_type = 'cex') THEN FROM_ADDRESS
    WHEN TO_ADDRESS IN (select address from thorchain.core.dim_labels WHERE label_type = 'cex') THEN To_ADDRESS
    End as cex,
    date_trunc('month', BLOCK_TIMESTAMP) AS DATE,
    CASE
    WHEN FROM_ADDRESS IN (select address from thorchain.core.dim_labels WHERE label_type = 'cex') THEN Amount_E8/pow(10,8)
    WHEN TO_ADDRESS IN (select address from thorchain.core.dim_labels WHERE label_type = 'cex') THEN -Amount_E8/pow(10,8)
    End as netflowusd
    FROM thorchain.core.fact_transfer_events
    WHERE BLOCK_TIMESTAMP BETWEEN {{start_date}} AND {{end_date}}
    AND asset='THOR.RUNE'
    AND len(netflowusd)>0
    ),

    Labels AS(
    select address as cex,
    label
    from thorchain.core.dim_labels
    WHERE label_type = 'cex'

    )

    SELECT
    l.label,
    sum(f.netflowusd) As Net_USD_transferred,
    f.Date
    From flows f
    LEFT JOIN labels l USING(cex)
    Group by 3,1
    QueryRunArchived: QueryRun has been archived