jackguyflows by cex
    Updated 2023-07-27

    with finalt as (
    SELECT
    address as a1,
    project_name
    from crosschain.core.address_labels
    WHERE label_type LIKE 'cex'
    AND blockchain like 'bitcoin'
    )

    SELECT
    PUBKEY_SCRIPT_ADDRESS,
    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
    PUBKEY_SCRIPT_ADDRESS,
    'out' as tx_type,
    sum(VALUE) as amount
    FROM bitcoin.core.fact_inputs
    -- LEFT outer JOIN finalt
    -- on PUBKEY_SCRIPT_ADDRESS LIKE a1
    WHERE PUBKEY_SCRIPT_ADDRESS IN (SELECT DISTINCT a1 from finalt)
    AND block_timestamp > current_date - 5
    GROUP BY 1

    UNION

    SELECT
    PUBKEY_SCRIPT_ADDRESS,
    'in' as tx_type,
    sum(VALUE) as amount
    FROM bitcoin.core.fact_outputs
    -- LEFT outer JOIN finalt
    Run a query to Download Data