jackguy2023-07-27 03:44 PM copy
    Updated 2023-10-31
    -- forked from 2023-07-27 03:44 PM @ https://flipsidecrypto.xyz/edit/queries/829e58f2-e48d-472f-ae57-e3c578cbd1c1

    with finalt as (
    SELECT DISTINCT address
    from bitcoin.core.dim_labels
    where label_type like 'cex'
    )


    SELECT
    *,
    sum(net_inflow) over (ORDER by day) as total_btc_flow

    FROM (
    SELECT
    day,
    sum(CASE when tx_type LIKE 'out' then amount end) as outflow,
    sum(CASE when tx_type LIKE 'in' then amount end) as inflow,
    sum(CASE when tx_type LIKE 'in' then amount end) - sum(CASE when tx_type LIKE 'out' then amount end) as net_inflow

    from (
    SELECT
    date_trunc('day', block_timestamp) as day,
    'out' as tx_type,
    sum(VALUE) as amount
    FROM bitcoin.core.fact_inputs
    WHERE PUBKEY_SCRIPT_ADDRESS IN (SELECT DISTINCT * from finalt)
    AND block_timestamp > current_date - 90
    GROUP BY 1

    UNION

    SELECT
    date_trunc('day', block_timestamp) as day,
    'in' as tx_type,
    sum(VALUE) as amount
    Run a query to Download Data