Flipside Query Tips and Tricks

Dates

select date_trunc('month', block_timestamp) from from terra.daily_balances
Hey! I'd like to delete only 2 days from a query where I take a full month.
SELECT days, amount 
FROM table.example 
WHERE days > '2021-10-01' 
AND days < '2021-11-01'

I wouldn't wanted to get 2021-11-14 and 2021-11-15  (Just an example)
AND days NOT IN ( '2021-11-14', ' 2021-11-15')

Sum of Previous Fields Daily

WITH DAILY_BORROW as (
  select 
  date(block_timestamp) as ddate,
  sum(amount_usd) as daily_borrow
  from anchor.borrows
where block_timestamp > CURRENT_DATE - 365
group by ddate
), DAILY_REPAY as (
select 
  date(block_timestamp) as ddate,
  sum(amount_usd) as daily_repay
  from anchor.repay
where block_timestamp > CURRENT_DATE - 365
group by ddate
)

select 
  b.ddate,
  daily_borrow,
  -daily_repay,
  sum(daily_borrow - daily_repay) over (order by b.ddate) as net_borrow
  from DAILY_BORROW b, DAILY_REPAY r
where b.ddate = r.ddate

Selecting Values with numbers in them

Handle Null Values

regexp_substr Example

Flatten terra.msg_events

dealing with JSON THINGS

Share Plots quickly with others

Smooth Charts

Joining Addresses To Labels

Lag Example

PIVOT (Rows to Columns): thanks sam#0575

UNPIVOT (Columns to Rows): thanks sam#0575

Generate Dates

Ranking Rows or Generating Row Numbers

Lag Example

Negative Numbers - ffffffffffffffffffffffffffff659843769458

Last updated