theericstonenet borrowing usd
    Updated 2022-07-07
    with borrowing as (select
    date_trunc('day',block_timestamp) as block_date,
    sum(amount_usd) as borrowing_usd
    from anchor.borrows b
    group by 1),
    repayment as (select
    date_trunc('day',block_timestamp) as block_date,
    sum(amount_usd) as repayment_usd
    from anchor.repay b
    group by 1),
    luna_price as (select
    date_trunc('day',block_timestamp) as block_date,
    symbol,
    avg(price_usd) as price_usd
    from terra.oracle_prices
    where symbol = 'LUNA'
    group by 1, 2
    ),
    luna_price_change as (select
    one.block_date,
    two.block_date as previous_block_date,
    ((one.price_usd - two.price_usd) / (two.price_usd)) * 100 as percentage_change
    from luna_price one
    inner join luna_price two
    on one.block_date = dateadd(day, 1, two.block_date)
    )

    select b.block_date,
    sum(borrowing_usd) over (order by b.block_date) as borrowing_usd,
    sum(repayment_usd) over (order by b.block_date) as repayment_usd,
    sum(borrowing_usd-repayment_usd)over (order by b.block_date) as net_borrowing_usd
    from borrowing b
    inner join repayment r
    on b.block_date = r.block_date
    where b.block_date > current_date - 180
    Run a query to Download Data