brian-terraEnterprise - Retention Fee Amount Based (%)
    Updated 2023-10-26
    -- forked from 0xHaM-d / Retention Fee Amount Based (%) @ https://flipsidecrypto.xyz/0xHaM-d/q/HZ5vAD1uCwzA/retention-fee-amount-based
    WITH create_daos as
    (select distinct tx_id from terra.core.fact_msg_attributes_standard
    where attribute_value = 'terra1y2dwydnnnctdwwmvs23ct60fj626t66qk53cae2gc55k3ce92jmqldj0sf')
    ,
    contracts as (
    select distinct attribute_value as contract
    from terra.core.fact_msg_attributes_standard a, create_daos b
    where a.tx_id = b.tx_id
    and attribute_key = '_contract_address'
    )
    ,
    txns as (
    select distinct tx_id
    from terra.core.fact_msg_attributes_standard a, contracts b
    where attribute_value = contract
    )

    ,
    qmain as (
    select block_timestamp,
    a.tx_id,
    CASE WHEN attribute_key = 'spender' then attribute_value end as address
    from terra.core.fact_msg_attributes_standard a, txns b
    where a.tx_id = b.tx_id
    and address is not null)
    ,
    qmain1 as (
    select b.block_timestamp,
    a.tx_id,
    b.address,
    CASE WHEN attribute_key = 'fee' then attribute_value end as fee
    from terra.core.fact_msg_attributes_standard a, qmain b
    where a.tx_id = b.tx_id
    and address is not null
    and fee is not null
    Run a query to Download Data