jackguy2023-07-27 03:44 PM
    Updated 2023-10-31
    with finalt as (
    SELECT DISTINCT address
    from crosschain.core.address_labels
    WHERE label_type LIKE 'cex'
    AND blockchain like 'bitcoin'
    )


    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
    FROM bitcoin.core.fact_outputs
    Run a query to Download Data