Nige7777Anchor Repayments totals 19
    Updated 2022-07-07

    with cte_b as (
    select
    SUM(msg_value:execute_msg:borrow_stable:borrow_amount / pow(10,6)) as Amount_Borrow,
    '1' as Type,
    date_trunc(day, block_timestamp) AS Day
    from terra.msgs where 1=1 --and tx_id in ( '7920C252C381D6D3952421133A7090D9C1D2079663B1E2E9A253711FA7FFAE06','1EB5176A40308B36C26564FA12B897AAE869B4E58D9ACFC3012EFB10FFE60A74')
    and msg_value:contract::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s'
    and msg_value:execute_msg:borrow_stable is not null
    and tx_status = 'SUCCEEDED'
    GROUP by Day
    order by day
    ),
    cte_r as (
    select
    SUM(msg_value:coins[0]:amount / pow(10,6)) as Amount_Repay,
    '1' as Type,
    date_trunc(day, block_timestamp) AS Day
    from terra.msgs where 1=1 --and tx_id in ( '7920C252C381D6D3952421133A7090D9C1D2079663B1E2E9A253711FA7FFAE06','1EB5176A40308B36C26564FA12B897AAE869B4E58D9ACFC3012EFB10FFE60A74')
    and msg_value:contract::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s'
    and msg_value:execute_msg:repay_stable is not null
    and tx_status = 'SUCCEEDED'
    GROUP by Day
    order by day
    )
    select distinct
    sum(Amount_borrow) over (order by b.day asc rows between unbounded preceding and current row) as runningBorrowTotal,
    sum(Amount_borrow - amount_repay) over (order by b.day asc rows between unbounded preceding and current row) as runningTotal,
    sum(amount_repay) over (order by b.day asc rows between unbounded preceding and current row) as runningRepayTotal,
    b.day
    from cte_b b
    left join cte_r r on r.day = b.day
    order by b.day



    Run a query to Download Data